不了解Android中的java.lang.ClassCastException错误

时间:2012-08-28 08:14:12

标签: android casting classcastexception runtimeexception

我通过Acra收到了Android应用程序的错误报告。堆栈跟踪中的代码中没有引用行号。我怎么知道问题出在我的代码上?有人可以帮助我。

堆栈追踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itse.htsurvey/com.itse.htsurvey.Question2Page2Activity}: java.lang.ClassCastException: android.widget.CompoundButton$SavedState cannot be cast to android.widget.AbsSpinner$SavedState
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3188)
        at android.app.ActivityThread.access$600(ActivityThread.java:122)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1028)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:132)
        at android.app.ActivityThread.main(ActivityThread.java:4123)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.CompoundButton$SavedState cannot be cast to android.widget.AbsSpinner$SavedState
        at android.widget.AbsSpinner.onRestoreInstanceState(AbsSpinner.java:421)
        at android.view.View.dispatchRestoreInstanceState(View.java:8316)
        at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:2038)
        at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:766)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.View.restoreHierarchyState(View.java:8295)
        at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1489)
        at android.app.Activity.onRestoreInstanceState(Activity.java:898)
        at android.app.Activity.performRestoreInstanceState(Activity.java:870)
        at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1099)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1793)
        ... 12 more
java.lang.ClassCastException: android.widget.CompoundButton$SavedState cannot be cast to android.widget.AbsSpinner$SavedState
        at android.widget.AbsSpinner.onRestoreInstanceState(AbsSpinner.java:421)
        at android.view.View.dispatchRestoreInstanceState(View.java:8316)
        at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:2038)
        at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:766)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
        at android.view.View.restoreHierarchyState(View.java:8295)
        at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1489)
        at android.app.Activity.onRestoreInstanceState(Activity.java:898)
        at android.app.Activity.performRestoreInstanceState(Activity.java:870)
        at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1099)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1793)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3188)
        at android.app.ActivityThread.access$600(ActivityThread.java:122)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1028)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:132)
        at android.app.ActivityThread.main(ActivityThread.java:4123)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
        at dalvik.system.NativeStart.main(Native Method)

我的代码com.itse.htsurvey.Question2Page2Activity:http://pastebin.com/gPYyZ0GH

编辑1:更多信息 - 我认为这可能是因为小部件ID冲突。 savedState部分表示这是在Activity重放后发生的?此布局由动态添加的视图/布局组成,并在运行时为其提供id。

编辑2:只是为了确认在测试中,这是我第一次收到此错误。我无法复制它。

编辑3:当我改变设备的方向时,我能够重现错误。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:3)

在阅读onSaveInstanceState和onRestoreInstanceState之后。我添加了以下代码。我已经保存了所有视图的状态,因此不需要onSaveInstanceState和onRestoreInstanceStat的默认实现。

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        saveEnteredData();  
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {

    }

答案 1 :(得分:2)

令人困惑的一件事是,在代码中,它看起来像是通过其原始id int引用视图。我实际上并不完全确定你的代码是做什么但是...这可能会搞砸了并导致Android找到错误的视图(也许这就是为什么你得到关于你甚至没有使用的视图的一些错误的错误,像AbsSpinner)。例如,代码使用:

CheckBox cb1 = (CheckBox) ll2_8.getChildAt(i).findViewById(R.id.tl1).findViewById(R.id.tr1).findViewById(i);

所以不要像这样做,你的最后一个“findViewById(i)”应该引用像你用“findViewById(R.id.tr1)”那样通过引用生成的东西。

验证,删除Eclipse中“gen”文件夹中的R.java,清理项目,构建项目,看看是否可以重现错误。你的应用应该(?)崩溃

答案 2 :(得分:0)

看起来你无法将复合按钮强制转换为Spinner。您只能将复合按钮用作CheckBox,RadioButton,Switch,ToggleButton,而不能使用Spinner。

答案 3 :(得分:0)

您正在尝试使用

  

android.widget.CompoundButton $ SavedState

对象而不是

  

android.widget.AbsSpinner $ SavedState。

尝试检查导入的类是否正确导入了适当的类。