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

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


当前回答

我有同样的问题,当做片段事务,而活动创建。

核心问题是什么尼克已经指出-视图树还没有膨胀。但是他的解决方案并没有起作用——在onResume, onPostCreate等中出现了同样的异常。

解决方案是在容器片段中添加回调,以便在它准备好时发出信号:

public class MyContainerFragment extends Fragment {
    public static interface Callbacks {
        void onMyContainerAttached();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(TAG, "--- onAttach");
        ((Callbacks) activity).onMyContainerAttached();
    }

    //... rest of code
}

然后在活动中:

public class MainActivity extends Activity
        implements MyContainerFragment.Callbacks
{
    @Override
    public void onMyContainerAttached() {
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.containerFrame, new MyFragment())
                .commit();
    }

    //...
}

其他回答

我也有同样的问题,但我的问题发生在取向改变上。其他的解决方案都不起作用。结果是我忘记删除setRetainInstance(true);从我的片段,当做一个基于屏幕大小的两个或一个窗格布局。

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

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

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

当你没有在app_bar_main.xml中放入<include layout="@layout/your_fragment_layout"/>时,也会发生这个问题

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

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

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

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