IllegalArgumentException:错误的州级

jch*_*tof 5 android illegalargumentexception

对于Activity,我有两个不同的纵向和横向布局文件.一个方向的元素与另一个方向的元素有直接关系,除了它们可能与基类相关但不是完全相同的类型,并且它们具有相同的id.例如:

布局/ main_layout.xml:

...
<ListView
 android:id="@+id/current_news_list"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

然后在layout-land/main_layout.xml中:CustomListView是android.widget.AdapterView的子类

...
<CustomListView 
 android:id="@+id/current_news_list"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

更改方向时会抛出"IllegalArgumentException:错误的状态类".这是预期的行为吗?我没有覆盖配置更改代码,我让活动被彻底销毁和重建.我已避免在布局层次结构中同时具有相同标识符的其他实例.

Kum*_*bek 11

将视图的值saveEnabled设置为false.

http://developer.android.com/reference/android/view/View.html#attr_android:saveEnabled

在更改方向时,它会尝试保存具有ID的视图的状态,并尝试在重新创建活动时重新创建相同的视图.因此,对于您的情况,一种类型无法转换为另一种类型.即.ListView无法转换为CustomListView.

然后,你将不得不自己处理这两个方向.