【问题标题】:Android: ID clash when 3 tabs or more are createdAndroid:创建 3 个或更多选项卡时 ID 冲突
【发布时间】:2014-08-16 06:33:26
【问题描述】:

我在 Android 中有一个选项卡式视图,这些选项卡共享相同的 xml 布局,它们是基于 JSON 动态生成的。例如,如果我有一个菜单,它将创建一个选项卡。两个菜单,两个选项卡。等等。 由于所有选项卡都基于相同的“菜单”对象,因此它们共享相同的扩展 xml。

标签的构造没有问题。当有一个或两个选项卡时,可以完美地更改状态和滑动。当拥有三个或更多选项卡时,向右滑动可以正常工作,但每当我向左滑动时,都会出现此错误:

java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.AbsListView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/header_list_view. Make sure other views do not use the same id.

这是我的 onCreateView 代码:

public class MenuToListFragment extends Fragment {

HeaderListView headerListView;

// (most of the logic removed)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
    headerListView = (HeaderListView) rootView.findViewById(R.id.header_list_view);
    return rootView;
}
}

这是我的 xml 布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="<removed>.DisplayMenus">


    <com.applidium.headerlistview.HeaderListView
        android:id="@+id/header_list_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</LinearLayout>

感谢您的帮助!

【问题讨论】:

    标签: android xml listview tabs


    【解决方案1】:

    好的,找到问题了! 使用方法 OnActivityCreated,在调用 HeaderListView 时,我必须使用 headerListView.setId() 分配一个唯一的 Id;对于此 id 的值,我必须使用 setArguments 和 getArguments 将选项卡的位置通过捆绑传递。我尚未探索的一种方法是使用 headerListView.setId(headerListView.generateViewId()) 生成 Id。

    现在,关于“为什么”它仅在具有 3 个或更多视图时发生。这就是我猜测的地方,我希望得到一些反馈。当给一个标签充气时,我发现只有邻居被保存在内存中(另一个被莫名其妙地丢弃了?)。如果两个选项卡,向左或向右移动将不会让程序生成或恢复视图,因为集合(相邻使用选项卡)总是相同的。现在,如果我有 3 个标签并一直向右滑动,第一个标签就会进入睡眠状态。滑回第二个选项卡会强制 android 将第一个选项卡从其睡眠状态恢复。因为休眠的选项卡与所有其他选项卡具有相同的 id,所以会产生冲突。瞧!

    希望这可以帮助遇到同样问题的人

    【讨论】:

    • 耶!谢谢你。我猜 HeaderListView 有一些奇怪的问题 XD
    【解决方案2】:

    我更改了布局定义:

    <com.applidium.headerlistview.HeaderListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:tag="HeaderListView" />
    

    然后使用标签查找视图:

    mHeaderListView = (HeaderListView) view.findViewWithTag("HeaderListView"); 
    

    它似乎有效,不再是java.lang.IllegalArgumentException

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多