Android Fragment no view found for ID?

前端 未结 30 1985
逝去的感伤
逝去的感伤 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:49

    I fixed this bug, I use the commitNow() replace commit().

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

    The commitNow is a sync method, the commit() method is an async method.

    0 讨论(0)
  • 2020-11-22 05:51

    I know this has already been answered for one scenario, but my problem was slightly different and I thought I'd share in case anybody else is in my shoes.

    I was making a transaction within onCreate(), but at this point the view tree has not been inflated so you get this same error. Putting the transaction code in onResume() made everything run fine.

    So just make sure your transaction code runs after the view tree has been inflated!

    0 讨论(0)
  • 2020-11-22 05:51

    I had the same problem it was caused because I tried to add fragments before adding the container layout to the activity.

    0 讨论(0)
  • 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<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;
    }
    

    }

    Hope this was helpful !!

    0 讨论(0)
  • 2020-11-22 05:53

    I was having this problem too, until I realized that I had specified the wrong layout in setContentView() of the onCreate() method of the FragmentActivity.

    The id passed into FragmentTransaction.add(), in your case R.id.feedContentContainer, must be a child of the layout specified in setContentView().

    You didn't show us your onCreate() method, so perhaps this is the same problem.

    0 讨论(0)
  • 2020-11-22 05:54

    Just in case someone's made the same stupid mistake I did; check that you're not overwriting the activity content somewhere (i.e. look for additional calls to setContentView)

    In my case, due to careless copy and pasting, I used DataBindingUtil.setContentView in my fragment, instead of DataBindingUtil.inflate, which messed up the state of the activity.

    0 讨论(0)
提交回复
热议问题