android - PagerAdapter 页面上带有 fragment 的重复 ID

标签 android android-fragments

我遇到了 pageradapter 和 fragment 的问题。

我有一个 PagerAdapter,我想在每个页面上放置一个 fragment 。在页面的 xml 布局上,我使用此代码添加 fragment :

<fragment android:id="@+id/fragment1"
    android:name="de.worldcup.android.ui.fragments.GroupTableFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

当我运行应用程序时,当我尝试在该行实例化 PagerAdapter 的第二页(第二次调用 instantiateItem 方法)时,当我尝试扩展页面的 xml 布局时,它崩溃了。

View v = inflater.inflate(R.layout.group_activity_item, null);

错误:

03-15 21:57:34.795: E/AndroidRuntime(6257): Caused by: java.lang.IllegalArgumentException: Binary XML file line #20: Duplicate id 0x7f050043, tag null, or parent id 0x0 with another fragment for de.worldcup.android.ui.fragments.GroupTableFragment

我查找了 id 0x7f050043 -> 它是 @+id/fragment1 id。

有什么解决办法吗?

谢谢:)

最佳答案

我自己遇到了同样的问题,我发现了这个: “注意:当布局包含 . 时,您不能将布局扩展到 fragment 中。只有在动态添加到 fragment 时才支持嵌套 fragment 。” In the documentation.简而言之,动态创建 fragment 架构来解决问题。

关于android - PagerAdapter 页面上带有 fragment 的重复 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9728164/

相关文章:

java - 我收到以下运行时错误。请帮我找出原因

Android从网络加载图像到viewpager

android - 如何将 Assets 文件转换为 XML 文件

android - 在 fragment 中实现对话框时,必须在添加内容之前请求窗口功能

java - 如何在 DialogFragment 的 textView 中 setText()

android - Robotium:如何重新签署 .apk 应用程序以便使用 Robotium 对其进行测试?

android - 如何比较字符串数组

android - fragment 测试错误 : android. view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class <unknown>

Android fragment 从布局资源中膨胀,在顶部显示一个额外的 strip

android - 在FrameLayout中动态添加多个 fragment