Android Fragment 没有找到 ID 的视图? [英] Android Fragment no view found for ID?

查看:33
本文介绍了Android Fragment 没有找到 ID 的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个片段添加到视图中.

I have a fragment I am trying to add into a view.

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");
}

执行此代码时,我在调试中收到以下错误..

When this code is executed I get the following error in debug..

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

feed_pa​​rser_activity 是一个 Fragment,在 xml 中设置为 Fragment 布局.
我正在使用 FragmentActivity 来托管包含 feed_pa​​rser_layout 的 Fragment 布局.
我上面的编码正确吗?

feed_parser_activity is a Fragment that is set to Fragment layout in xml.
I am using a FragmentActivity to host the Fragment Layout holding the feed_parser_layout.
Am I coding this correctly above?

推荐答案

解决方案是使用 getChildFragmentManager()

代替 getFragmentManager()

从片段调用时.如果您从活动中调用该方法,请使用 getFragmentManager().

when calling from a fragment. If you are calling the method from an activity, then use getFragmentManager().

这样就能解决问题了.

这篇关于Android Fragment 没有找到 ID 的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆