"无法将TextView强制转换为ToggleButton",但它是一个ToggleButton

JJJ*_*jim 1 android textview togglebutton

我是Android开发的新手,所以如果这是一个"noobish"的问题,我会事先提前通知.

使用RelativeLayout,我有一个ID为的ToggleButton reminderToggle:

<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" />
Run Code Online (Sandbox Code Playgroud)

我在用:

ToggleButton reminderToggle = (ToggleButton)findViewById(R.id.reminderToggle);
Run Code Online (Sandbox Code Playgroud)

然后:

if(reminderToggle.isChecked()) {
Run Code Online (Sandbox Code Playgroud)

在一个按钮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)
Run Code Online (Sandbox Code Playgroud)

第139行是:

ToggleButton reminderToggle = (ToggleButton)findViewById(R.id.reminderToggle);
Run Code Online (Sandbox Code Playgroud)

正如您在XML中看到的,reminderToggle是一个ToggleButton.所有这些大惊小怪的是什么?:)

在此先感谢,JJ56

PS:提醒切换的ID只有一个,就是这个ToggleButton

san*_*san 7

清理并重建它.还要确保自动检查构建以避免此类问题.