我是 Android 开发的新手,所以如果这是一个“菜鸟”问题,我提前表示歉意。
使用 RelativeLayout,我有一个 ID 为 reminderToggle
的 ToggleButton:
<ToggleButton
android:id="@+id/reminderToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/reminderDesc"
android:layout_alignBottom="@+id/reminderDesc"
android:layout_toRightOf="@+id/imageView1"
android:text="ToggleButton" />
我正在使用:
ToggleButton reminderToggle = (ToggleButton)findViewById(R.id.reminderToggle);
然后:
if(reminderToggle.isChecked()) {
在按钮onClick
监听器内,在onCreate
内,可以看到ToggleButton 被选中。这一切都很好。我不知道我做了什么,但现在单击按钮时应用程序崩溃了。在 LogCat` 中,我可以看到
FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ToggleButton
at nz.co.kwiius.park.ParkActivity$4.onClick(ParkActivity.java:139)
第 139 行是:
ToggleButton reminderToggle = (ToggleButton)findViewById(R.id.reminderToggle);
如您在 XML 中所见,reminderToggle 是一个 ToggleButton。那么这些大惊小怪的是什么? :)
提前致谢, JJ56
PS:id为reminderToggle的东西只有一个,就是这个ToggleButton
最佳答案
伙计,清理并重建它。还要确保始终自动检查构建以避免此类问题。
关于android - "Cannot cast TextView to ToggleButton",但它是一个 ToggleButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8529788/