屏幕方向更改期间,onRestoreInstanceState内的AbsListView:ClassCastException

时间:2013-05-09 21:29:59

标签: android android-listview android-2.3-gingerbread

我在几个片段和活动中使用了AbsListView,因此我可以在纵向模式下使用ListView,在横向模式中使用GridView。我在ICS和JB中进行了所有日常测试,这很好用。我开始在GB设备(2.3.6)上进行测试,当我在其中一个屏幕处于活动状态时旋转设备时,我得到一个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 个答案:

答案 0 :(得分:1)

由于该视图根据方向具有不同的类型,因此不需要保存状态。通过setSaveEnabled停用它会从onSaveInstanceState / onRestoreInstanceState事件中移除此控件。

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