2012-08-28 61 views
1

我通過Acra收到Android應用程序的錯誤報告。在我的代碼中沒有提到堆棧跟蹤中的行號。我怎麼知道問題在於我的代碼?任何人都可以幫助我。不理解Android中的java.lang.ClassCastException錯誤

堆棧跟蹤:

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:一些詳細信息 - 我想這可能是因爲一個部件編號衝突。 savedState部分表明這發生在一個活動休閒之後?這個佈局由動態添加的視圖/佈局組成,它們在運行時被賦予了id。

編輯2:只是爲了確認在所做的測試中,這是我第一次收到這個錯誤。我一直無法重現它。

編輯3:當我改變設備的方向時,我能夠重現錯誤。我如何解決這個問題?

回答

3

讀完onSaveInstanceState和onRestoreInstanceState後。我添加了下面的代碼。我已經保存了所有視圖的狀態,所以不需要onSaveInstanceState和onRestoreInstanceStat的默認實現。

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

    @Override 
    public void onRestoreInstanceState(Bundle savedInstanceState) { 

    } 
0

看起來像不能將複合按鈕投射到微調器。您只能將複合按鈕用作CheckBox,RadioButton,Switch,ToggleButton,但不能使用Spinner。

0

您正在嘗試使用

android.widget.CompoundButton $ SavedState

對象,而不是

android.widget.AbsSpinner $ SavedState。

只要嘗試檢查導入的類是否有正確導入適當的類。

+0

+1的思維過程,但我還是不明白,因爲他甚至沒有在所有 –

+0

我使用自定義的微調實現使用AbsSpinner – Rynardt

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,清理項目,構建項目並查看是否可以重現錯誤。你的應用程序應該(?)崩潰

+1

我給ID的在運行時的觀點,因爲我動態膨脹意見在運行期間。在運行時訪問動態創建的視圖的唯一方法是在運行時分配一個id? – Rynardt

+0

嗯...沒關係。好吧,它可能無法解決你的問題,但還有另一種方法來做到這一點。如果你只是在一個ArrayList或其他容器中保留對視圖的引用,那可能會使事情變得更安全。 –

+0

ArrayList checkBoxes = new ArrayList ();對於(int i = 0; i <10; i ++) public void initPage(){ checkBoxes.add } //現在你可以參考你班上任何地方的任何複選框...... } –

相關問題