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

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


当前回答

在我的例子中,我在回收器视图项中有一个SupportMapFragment(我使用了较低开销的“liteMode”,它使地图看起来是非交互式的,几乎像一个静态图像)。我使用了正确的FragmentManager,一切看起来都很正常……用一个小列表。当项目列表超过屏幕高度时,我在滚动时开始遇到这个问题。

Turned out, it was because I was injecting a dynamic SupportMapFragment inside a view, which was inside another fragment, to get around some issues I was having when trying to declare it statically in my XML. Because of this, the fragment placeholder layout could only be replaced with the actual fragment once the view was attached to the window, i.e. visible on screen. So I had put my code for initialising the SupportMapFragment, doing the Fragment replace, and calling getMapAsync() in the onAttachedToWindow event.

我忘记做的是确保我的代码不会运行两次。例如,在onAttachedToWindow事件中,在尝试创建它的新实例并进行片段替换之前,检查我的动态SupportMapFragment是否仍然为空。当项目离开RecyclerView顶部时,它将从窗口中分离,然后当您回滚到它时重新连接,因此此事件将触发多次。

一旦我添加了空检查,它只发生一次每个RecyclerView项目和问题消失了!TL;博士!

其他回答

在我的情况下,我试图显示一个包含页导航的DialogFragment,当FragmentPagerAdapter试图将片段添加到页导航时,这个异常被抛出。根据howettl的回答,我猜这是由于寻页器的父母不是在setContentView()在我的FragmentActivity的视图设置。

我唯一做的改变来解决这个问题是创建FragmentPagerAdapter通过调用getChildFragmentManager()获得的FragmentMager,而不是通过调用getFragmentManager()获得的,因为我通常这样做。

    public class PagerDialog extends DialogFragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.pager_dialog, container, false);

        MyPagerAdapter pagerAdapter = new MyPagerAdapter(getChildFragmentManager());
        ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
        pager.setAdapter(pagerAdapter);

        return rootView;
    }
}

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

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

    //...
}

在我们的例子中,我们清除/损坏了class.dex文件以及gradle编译的输出。由于缓存重新编译没有尝试,因此没有错误,但apk没有所需的文件,导致这个令人困惑的错误。一个gradle resync和新的构建清除了我们所有的错误。

//快乐编码

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

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

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