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

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的视图时,也会发生这种情况

其他回答

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

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

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

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

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

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

当有嵌套片段并使用getSupportFragmentManager()而不是getChildFragmentManager()添加它们时,也会发生此错误。

正如https://stackoverflow.com/users/2075875/malachiasz所言

这个解决方案实际上是可行的

getChildFragmentManager().beginTransaction().replace(R.id.cpu_bottomNav_frame,new fragment()).commit();

对我来说。我有一个活动与几个片段 有时候我需要重新创建碎片的时候 语言就是变化 片段布局有的需要有的不需要或者内容变更需要重新创建 其他的变化

我清除所有的片段,并设置所有为空的活动 但是Fragment已经创建了自己,当它承载活动时 是bean集空,所以之前调用片段视图 检查为空

例如

Activity{
    fragment
    recreate{
       fragment = null then new instance
    }

}

Fragment{
    if((activity).fragment != null) {
       findViewById()
    }

}