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

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;博士!

其他回答

我也有同样的问题,让我把我的代码贴出来让你们都能看到,不要做和我一样的事情。

@Override
protected void onResume()
{
    super.onResume();

    fragManager = getSupportFragmentManager();

    Fragment answerPad=getDefaultAnswerPad();
    setAnswerPad(answerPad);
    setContentView(R.layout.abstract_test_view);
}
protected void setAnswerPad(AbstractAnswerFragment pad)
{
    fragManager.beginTransaction()
        .add(R.id.AnswerArea, pad, "AnswerArea")
        .commit();
    fragManager.executePendingTransactions();
}

注意,我在setContentView之前设置了片段。糟糕。

当我试图用onCreateView()中的片段替换视图时,我遇到了这个问题。是这样的:

public class MyProjectListFrag extends Fragment {


    private MyProjectListFragment myProjectListFragment;

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

        FragmentManager mFragmentManager = getFragmentManager();
        myProjectListFragment = new MyProjectListFragment();
        mFragmentManager
                .beginTransaction()
                .replace(R.id.container_for_my_pro_list,
                        myProjectListFragment, "myProjectListFragment")
                .commit();
    }

它告诉我

11-25 14:06:04.848: E/AndroidRuntime(26040): java.lang.IllegalArgumentException: No view found for id 0x7f05003f (com.example.myays:id/container_for_my_pro_list) for fragment MyProjectListFragment{41692f40 #2 id=0x7f05003f myProjectListFragment}

然后我修复了这个问题,把替换到onActivityCreated()。是这样的:

public class MyProjectListFrag extends Fragment {

    private final static String TAG = "lch";

    private MyProjectListFragment myProjectListFragment;

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

        return inflater
                .inflate(R.layout.frag_my_project_list, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        FragmentManager mFragmentManager = getFragmentManager();
        myProjectListFragment = new MyProjectListFragment();
        mFragmentManager
                .beginTransaction()
                .replace(R.id.container_for_my_pro_list,
                        myProjectListFragment, "myProjectListFragment")
                .commit();

    }

你必须在onCreateView()中返回一个视图,以便稍后替换它 你可以在fragment liftcycle中的以下函数中对这个视图进行任何操作,比如onActivityCreated()

希望这能有所帮助!

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

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

在我的例子中,我在回收器视图项中有一个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;博士!

在回收器视图中使用Viewpager时,我面临一个严重的错误。 下面是我在特殊情况下遇到的错误。 我开始了一个片段,它有一个RecyclerView与Viewpager(使用FragmentStatePagerAdapter)。它工作得很好,直到我在RecyclerView中点击一个单元格切换到不同的片段,然后使用手机的硬件后退按钮导航回来,应用程序崩溃了。

有趣的是,我在同一个RecyclerView中有两个Viewpagers,它们都是大约5个单元格的距离(另一个在屏幕上不可见,它已经关闭了)。所以最初我只是应用了第一个Viewpager的解决方案,并留下了另一个,因为它是(Viewpager使用片段)。

当第一个查看分页器可见时,返回导航工作正常。现在,当我向下滚动到第二个,然后改变片段,回来,它崩溃了(同样的事情发生在第一个)。所以我不得不改变两个Viewpagers。

无论如何,阅读下面的内容,找到可行的解决方案。 崩溃错误如下:

java.lang.IllegalArgumentException: No view found for id 0x7f0c0098 (com.kk:id/pagerDetailAndTips) for fragment ProductDetailsAndTipsFragment{189bcbce #0 id=0x7f0c0098}

花了几个小时调试它。阅读这篇完整的帖子,直到底部应用所有的解决方案,包括确保我传递childFragmentManager。

毫无效果。

最后,我没有使用FragmentStatePagerAdapter,而是扩展了PagerAdapter,并在Viewpager中使用它,而不使用片段。我相信某些地方存在嵌套片段的BUG。不管怎样,我们有选择。读……

下面的链接非常有用:

没有片段的Viewpager

链接可能会死,所以我在这里张贴我实现的解决方案如下:

public class ScreenSlidePagerAdapter extends PagerAdapter {
private static final String TAG = "ScreenSlidePager";
ProductDetails productDetails;
ImageView imgProductImage;
ArrayList<Imagelist> imagelists;
Context mContext;

// Constructor
public ScreenSlidePagerAdapter(Context mContext,ProductDetails productDetails) {
    //super(fm);
    this.mContext = mContext;
    this.productDetails = productDetails;
}

// Here is where you inflate your View and instantiate each View and set their values
@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.product_image_slide_cell,container,false);

    imgProductImage = (ImageView) layout.findViewById(R.id.imgSlidingProductImage);
    String url = null;
    if (imagelists != null) {
        url = imagelists.get(position).getImage();
    }

    // This is UniversalImageLoader Image downloader method to download and set Image onto Imageview
    ImageLoader.getInstance().displayImage(url, imgProductImage, Kk.options);

    // Finally add view to Viewgroup. Same as where we return our fragment in FragmentStatePagerAdapter
    container.addView(layout);
    return layout;
}

// Write as it is. I don't know much about it
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
    /*super.destroyItem(container, position, object);*/
}

// Get the count
@Override
public int getCount() {
    int size = 0;

    if (productDetails != null) {
        imagelists =  productDetails.getImagelist();
        if (imagelists != null) {
            size = imagelists.size();
        }
    }
    Log.d(TAG,"Adapter Size = "+size);
    return size;
}

// Write as it is. I don't know much about it
@Override
public boolean isViewFromObject(View view, Object object) {

    return view == object;
}

}

希望这对你有帮助!!