2013-05-09 59 views
0

我在若干片段和活動中使用AbsListView,因此我可以在縱向模式下使用ListView,橫向中使用GridView。我在ICS和JB上做了所有的日常測試,而且工作得很好。我開始在GB設備上進行測試(2.3.6),並且當我使用其中一個屏幕激活時旋轉設備時出現ClassCastException。以下是堆棧跟蹤:AbsListView:屏幕方向更改期間的onRestoreInstanceState中的ClassCastException

05-09 14:04:55.019: E/AndroidRuntime(6052): Caused by: java.lang.ClassCastException: android.widget.ListView$SavedState 
05-09 14:04:55.019: E/AndroidRuntime(6052):  at android.widget.AbsListView.onRestoreInstanceState(AbsListView.java:1018) 
05-09 14:04:55.019: E/AndroidRuntime(6052):  at android.view.View.dispatchRestoreInstanceState(View.java:6325) 
05-09 14:04:55.019: E/AndroidRuntime(6052):  at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:1314) 
05-09 14:04:55.019: E/AndroidRuntime(6052):  at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:767) 
05-09 14:04:55.019: E/AndroidRuntime(6052):  at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1301) 
05-09 14:04:55.019: E/AndroidRuntime(6052):  at android.view.View.restoreHierarchyState(View.java:6304) 
05-09 14:04:55.019: E/AndroidRuntime(6052):  at android.support.v4.app.Fragment.restoreViewState(Fragment.java:417) 

回答

1

由於此視圖是取決於方向的不同類型,因此不需要保存狀態。通過setSaveEnabled禁用它將從onSaveInstanceState/onRestoreInstanceState事件中刪除此控件。

list = (AbsListView) findViewById(R.id.list); 
    list.setSaveEnabled(false);