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

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


当前回答

如果您试图用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);

其他回答

我也有这个问题,直到我意识到我在FragmentActivity的onCreate()方法的setContentView()中指定了错误的布局。

传入FragmentTransaction.add()的id,在您的示例中是R.id。feedContentContainer,必须是setContentView()中指定的布局的子元素。

您没有向我们展示onCreate()方法,因此这可能是相同的问题。

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

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

我有这个问题。在我的情况下,我忘记在我的Xml文件中添加框架布局,添加框架布局后,我的问题已经解决了。

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

只需添加viewPager。setOffscreenPageLimit(页面数量);

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

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

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

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

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

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