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

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


当前回答

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

参考2020年最新版本的android

其他回答

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

}

希望这对你有帮助!!

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

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

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

这个页面似乎是发布关于片段IllegalArgumentException的建议的一个很好的中心位置。还有一件事你可以试试。这是最后对我有用的方法:

我忘了我有一个单独的横向布局文件。在我添加了我的FrameLayout容器之后,这个片段也工作了。


On a separate note, if you have already tried everything else suggested on this page (and the entire Internet, too) and have been pulling out your hair for hours, consider just dumping these annoying fragments and going back to a good old standard layout. (That's actually what I was in the process of doing when I finally discovered my problem.) You can still use the container concept. However, instead of filling it with a fragment, you can use the xml include tag to fill it with the same layout that you would have used in your fragment. You could do something like this in your main layout:

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

    <include layout="@layout/former_fragment_layout" />

</FrameLayout>

其中former_fragment_layout是您试图在片段中使用的XML布局文件的名称。有关更多信息,请参见使用include重用布局。