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

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


当前回答

我的错误在于FragamentTransaction。

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

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

其他回答

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

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

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

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

例如

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

}

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

}

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

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

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

参考2020年最新版本的android

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

核心问题是什么尼克已经指出-视图树还没有膨胀。但是他的解决方案并没有起作用——在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();
    }

    //...
}