gpt4 book ai didi

Android:创建 3 个或更多选项卡时 ID 冲突

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:58:19 25 4
gpt4 key购买 nike

我在 Android 中有一个选项卡式 View ,选项卡共享相同的 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>

感谢您的帮助!

最佳答案

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

现在,关于只有在具有 3 个或更多 View 时才会发生的“原因”。这是我猜测的地方,我希望得到一些反馈。在对选项卡进行充气时,我发现只有邻居保留在内存中(其他的不知何故被丢弃了?)。如果有两个选项卡,向左或向右不会让程序生成或恢复 View ,因为集合(相邻使用选项卡)始终相同。现在,如果我有 3 个选项卡并一直向右滑动,第一个选项卡将进入休眠状态。滑回第二个选项卡会强制 android 将第一个选项卡从其 sleep 状态恢复。因为休眠选项卡与所有其他选项卡具有相同的 ID,所以会产生冲突。瞧!

希望对遇到同样问题的人有所帮助

关于Android:创建 3 个或更多选项卡时 ID 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25337592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com