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

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


当前回答

在回收器视图中使用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;
}

}

希望这对你有帮助!!

其他回答

以防有人和我犯同样愚蠢的错误;检查你没有覆盖活动内容的某处(即寻找额外的调用setContentView)

在我的例子中,由于粗心的复制和粘贴,我使用了DataBindingUtil。setContentView在我的片段,而不是DataBindingUtil。膨胀,这会打乱活动的状态。

有时是因为你在使用一个BottomNavigationView。如果你从导航中打开Intent,在那个活动中打开一个片段

transaction.replace(R.id.container,new YourFragment());

那么Activity将无法找到你正在使用的导航方法。

解决方案:将活动更改为片段,并在应用程序中使用addOnBackStack处理导航。如果你已经实现了Jetpack导航,只需在项目中使用片段。

如果您传递给FragmentTransaction的布局ID也会发生此异常。replace(int ID, fragment)存在于其他正在膨胀的布局中。确保布局ID是唯一的,它应该工作。

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

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