Android Fragment no view found for ID?

前端 未结 30 2005
逝去的感伤
逝去的感伤 2020-11-22 05:33

I have a fragment I am trying to add into a view.

FragmentManager fragMgr=getSupportFragmentManager();
feed_parser_activity content = (feed_parser_activity)f         


        
30条回答
  •  灰色年华
    2020-11-22 05:52

    I was facing a Nasty error when using Viewpager within Recycler View. Below error I faced in a special situation. I started a fragment which had a RecyclerView with Viewpager (using FragmentStatePagerAdapter). It worked well until I switched to different fragment on click of a Cell in RecyclerView, and then navigated back using Phone's hardware Back button and App crashed.

    And what's funny about this was that I had two Viewpagers in same RecyclerView and both were about 5 cells away(other wasn't visible on screen, it was down). So initially I just applied the Solution to the first Viewpager and left other one as it is (Viewpager using Fragments).

    Navigating back worked fine, when first view pager was viewable . Now when i scrolled down to the second one and then changed fragment and came back , it crashed (Same thing happened with the first one). So I had to change both the Viewpagers.

    Anyway, read below to find working solution. Crash Error below:

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

    Spent hours debugging it. Read this complete Thread post till the bottom applying all the solutions including making sure that I am passing childFragmentManager.

    Nothing worked.

    Finally instead of using FragmentStatePagerAdapter , I extended PagerAdapter and used it in Viewpager without Using fragments. I believe some where there is a BUG with nested fragments. Anyway, we have options. Read ...

    Below link was very helpful :

    Viewpager Without Fragments

    Link may die so I am posting my implemented Solution here below:

    public class ScreenSlidePagerAdapter extends PagerAdapter {
    private static final String TAG = "ScreenSlidePager";
    ProductDetails productDetails;
    ImageView imgProductImage;
    ArrayList 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;
    }
    

    }

    Hope this was helpful !!

提交回复
热议问题