IllegalArgumentException:错误的状态类
对于活动,我有两个不同的纵向和横向布局文件。一个方向的元素与另一方向的元素有直接关系,除了它们可能通过基类相关但不是完全相同的类型并且它们确实具有相同的 id。例如:
layout/main_layout.xml:
...
<ListView
android:id="@+id/current_news_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
然后在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"/>
更改方向时会引发“IllegalArgumentException:错误的状态类”。这是预期的行为吗?我没有覆盖配置更改代码,并且我让活动被完全销毁和重建。我避免了布局层次结构中同时具有相同标识符的其他情况。
For an Activity, I have two different layout files for portrait and landscape orientations. The elements of one orientation have direct relation to elements in the other orientation except that they may be related by base class but are not the exact same type and they do have the same id. So for instance:
layout/main_layout.xml:
...
<ListView
android:id="@+id/current_news_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
then in layout-land/main_layout.xml:
CustomListView is a subclass of android.widget.AdapterView
...
<CustomListView
android:id="@+id/current_news_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
"IllegalArgumentException: Wrong state class" is thrown when changing orientations. Is this the expected behavior? I have not overridden configuration changing code and I'm letting the activity be completely destroyed and reconstructed. I've avoided other instances of having identical identifiers in the layout hierarchy at the same time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将视图的值 saveEnabled 设置为 false。
http://developer.android.com/reference/android/view /View.html#attr_android:saveEnabled
在更改方向时,它会尝试保存具有 ID 的视图的状态,并在重新创建 Activity 时尝试重新创建相同的状态。因此,对于您的情况,一种类型无法转换为另一种类型。 IE。 ListView 无法转换为 CustomListView。
然后,你必须自己处理这两个方向。
Set the view's value saveEnabled to false.
http://developer.android.com/reference/android/view/View.html#attr_android:saveEnabled
While changing orientation, it tries to save the states of the views which have IDs, and tries to recreate the same while recreating your activity. So, for your case, one type cannot be converted into the other type. ie. A ListView can't be converted into a CustomListView.
And then, you will have to handle both the orientations yourself.