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

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的片段布局。 我上面写的对吗?


当前回答

在我的情况下,我使用了一个通用的片段持有人使用我的活动类,我在运行时用适当的片段替换这个通用片段。

问题是我给id的包括和generic_fragment_layout,从包括删除id解决了它。

其他回答

如果您传递给FragmentTransaction的布局ID也会发生此异常。replace(int ID, fragment)存在于其他正在膨胀的布局中。确保布局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> 

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

有点晚了,但可能会对某人有所帮助:

我压倒一切 onCreate (savedInstanceState:包?, persistentState: PersistableBundle? (自动完成首先建议这个,我在这里添加了setContentView())

请确保重写正确的代码: onCreate (savedInstanceState:包?)

当你从一个片段中调用另一个片段时,就会发生这种情况。

使用:

getActivity().getSupportFragmentManager().beginTransaction();

在我的例子中,我使用了一个片段类文件来声明一个listview适配器类。 我只是为公共适配器类使用了一个不同的文件,错误就消失了。