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

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

    //...
}

其他回答

在我的情况下,我试图显示一个包含页导航的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;
    }
}

我修复了这个错误,我使用commitNow()代替commit()。

mFragment.getChildFragmentManager()
  .beginTransaction()
  .replace(R.id.main_fragment_container,fragment)
  .commitNowAllowingStateLoss();

commitNow是一个同步方法,commit()是一个异步方法。

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

有点晚了,但可能会对某人有所帮助:

我压倒一切 onCreate (savedInstanceState:包?, persistentState: PersistableBundle? (自动完成首先建议这个,我在这里添加了setContentView())

请确保重写正确的代码: onCreate (savedInstanceState:包?)

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

//快乐编码