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

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


当前回答

我在我的项目中使用视图绑定,不注意在膨胀ActivityHelloWorldBinding类后添加setContentView():

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityHelloWorldBinding.inflate(layoutInflater)

    // Add this line.
    setContentView(binding.root)
}

其他回答

如果您传递给FragmentTransaction的布局ID也会发生此异常。replace(int ID, fragment)存在于其他正在膨胀的布局中。确保布局ID是唯一的,它应该工作。

我在另一个线程上读到的答案与此类似,当我遇到这个问题时,它涉及到布局xml。

你的日志显示“没有为id 0x7f080011找到视图”。

打开gen->包->R.java->id,然后查找id 0x7f080011。

当我遇到这个问题时,这个id属于activity_main.xml文件中的FrameLayout。

FrameLayout没有一个ID(没有声明android: ID = "blablabla")。

确保所有布局中的所有组件都有id,特别是logcat中引用的组件。

我从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> 

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

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

参考2020年最新版本的android

我的错误在于FragamentTransaction。

我在做t。replace(r。layout。mylayout);代替t.replace(R.id.mylayout);

区别在于一个是布局,另一个是对布局的引用(id)