我有一个片段,我试图添加到一个视图。

FragmentManager fragMgr=getSupportFragmentManager();
feed_parser_activity content = (feed_parser_activity)fragMgr
                                    .findFragmentById(R.id.feedContentContainer);
FragmentTransaction xaction=fragMgr.beginTransaction();

if (content == null || content.isRemoving()) {
    content=new feed_parser_activity(item.getLink().toString());
    xaction
        .add(R.id.feedContentContainer, content)
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        .addToBackStack(null)
        .commit();
    Log.e("Abstract", "DONE");
}

当这段代码执行时,我在调试中得到以下错误。

java.lang.IllegalArgumentException: No view found for id 0x7f080011 
   for fragment feed_parser_activity{41882f50 #2 id=0x7f080011}

feed_parser_activity是一个片段,在xml中被设置为片段布局。 我使用FragmentActivity来托管持有feed_parser_layout的片段布局。 我上面写的对吗?


当前回答

我也得到了同样的错误,然后,我意识到问题是。add(R.id.CONTAINERNAME)所以基本上我给了id = CONTAINERNAME在错误的xml .. 因此,解决方案是首先检查你已经给出了id = CONTAINERNAME在xml中,你想要显示的片段。 还要注意,我试图调用片段形式的活动.. 我希望这样的小错误能解决问题

参考2020年最新版本的android

其他回答

以防有人和我犯同样愚蠢的错误;检查你没有覆盖活动内容的某处(即寻找额外的调用setContentView)

在我的例子中,由于粗心的复制和粘贴,我使用了DataBindingUtil。setContentView在我的片段,而不是DataBindingUtil。膨胀,这会打乱活动的状态。

如果您试图用fragmentManager替换片段中的片段,但是您没有膨胀父片段,这可能会导致问题。

在BaseFragment.java中OnCreateView:

if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, new DifferentFragment())
                    .commit();
        }

return super.onCreateView(inflater, container, savedInstanceState);

取代超级。onCreateView(膨胀器,容器,savedInstanceState); 通过膨胀片段的正确布局:

        return inflater.inflate(R.layout.base_fragment, container, false);

我也得到了同样的错误,然后,我意识到问题是。add(R.id.CONTAINERNAME)所以基本上我给了id = CONTAINERNAME在错误的xml .. 因此,解决方案是首先检查你已经给出了id = CONTAINERNAME在xml中,你想要显示的片段。 还要注意,我试图调用片段形式的活动.. 我希望这样的小错误能解决问题

参考2020年最新版本的android

我有这个问题(在代码中构建我的UI),这是由我的ViewPager(显示片段)没有ID设置造成的,所以我简单地使用了page . setid (ID),然后它就工作了。

这篇文章帮我找到了答案。

我从com.android.support:support-v4:21.0.0升级到com.android.support:support-v4:22.1.1时遇到了这个错误。

我不得不改变我的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout> 

:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</FrameLayout> 

所以布局必须有一个子视图。我猜他们在新图书馆里强制这么做了。