2015-06-05 61 views
0

我想通過使用savedInstance和Bundle更改屏幕方向時嘗試補償我的列表項,但不斷收到多個java.lang.ClassNotFoundException錯誤。當我遍歷代碼時,它似乎將項目添加到包中,但是當我在調試模式下嘗試查看它時,它顯示java.lang.ClassNotFoundException錯誤。我想指出「ts」的可序列化值顯示相同的錯誤,但是當我旋轉屏幕時,原始時間會保持不變。找不到定向更改的已保存實例類

我真的不知道發生了什麼,任何幫助表示讚賞。

主要活動//

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_li_summary); 

    getWindow().setSoftInputMode(
      WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

     if (savedInstanceState != null) 
     { 
      if ((savedInstanceState != null) && (savedInstanceState.getSerializable("ts") != null)) 
      { 
       startTime = (Calendar) savedInstanceState.getSerializable("starttime"); 
       String samp = (String) savedInstanceState.getSerializable("ts"); 
       refreshTS = (TextView) findViewById(R.id.timeStamp); 
       refreshTS.setText(samp); 

       String[] values = savedInstanceState.getStringArray("listItems"); 
       if (values != null) { 
        for (int i = 0; i< values.length; i++) { 
         mMainListAdapter.add(values[i]); 
        } 
       } 
      } 

     } 
} 

savedInstance //

@Override 
protected void onSaveInstanceState(Bundle state) { 

    super.onSaveInstanceState(state); 
    state.putSerializable("starttime", startTime); 
    state.putSerializable("ts",refreshTS.getText().toString()); 

    ArrayList<String> values = new ArrayList<String>() ; 
    for (int i =0; i< mMainListAdapter.getCount(); i++) 
    { 
     String obj = mMainListAdapter.getItem(i); 
     values.add(obj); 
    } 

    state.putStringArrayList("listItems", values); 
} 

堆棧跟蹤//

06-05 07:27:08.394 13901-13901/commyapp.myapp W/Bundle﹕ Attempt to cast generated internal exception: 
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String[] 
      at android.os.Bundle.getStringArray(Bundle.java:1565) 
      at commyapp.myapp.Summary.onCreate(Summary.java:90) 
      at android.app.Activity.performCreate(Activity.java:5600) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2504) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2599) 
      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4218) 
      at android.app.ActivityThread.access$1000(ActivityThread.java:174) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:146) 
      at android.app.ActivityThread.main(ActivityThread.java:5748) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
      at dalvik.system.NativeStart.main(Native Method) 
+0

你有ClassNotFoundException的堆棧跟蹤? – lordoku

+0

不相關,並且不太可能與崩潰相關,您檢查savedInstanceState爲空兩次,可以刪除它。 – Zadrox

+0

您是否正確實現了Serializable? – Gosu

回答

3

onSaveInstanceState你存儲的ArrayList<String>listItems,但檢索listItems你的時候請使用getStringArray("listItems") h返回String[]

要檢索listItems你應該使用下列內容:

ArrayList<String> values = savedInstanceState.getStringArrayList("listItems"); 
+0

賓果!非常感謝你。 – snapplex