Android Fragment no view found for ID?

前端 未结 30 1988
逝去的感伤
逝去的感伤 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 06:07

    This exception can also happen if the layout ID which you are passing to FragmentTransaction.replace(int ID, fragment) exists in other layouts that are being inflated. Make sure the layout ID is unique and it should work.

    0 讨论(0)
  • 2020-11-22 06:07

    In my case this exception was thrown when I used different ids for the same layout element (fragment placeholder) while having several of them for different Build Variants. For some reason it works perfectly well when you are replacing fragment for the first time, but if you try to do it again you get this exception. So be sure you are using the same id if you have multiple layouts for different Build Variants.

    0 讨论(0)
  • 2020-11-22 06:07

    In my case, i was using a fragment class file to declare a listview adapter class. I just used a different file for the public adapter class and the error was gone.

    0 讨论(0)
  • 2020-11-22 06:08

    In my case I was trying to show a DialogFragment containing a pager and this exception was thrown when the FragmentPagerAdapter attempted to add the Fragments to the pager. Based on howettl answer I guess that it was due to the Pager parent was not the view set in setContentView() in my FragmentActivity.

    The only change I did to solve the problem was to create the FragmentPagerAdapter passing in a FragmentMager obtained by calling getChildFragmentManager(), not the one obtained by calling getFragmentManager() as I normally do.

        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;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:08

    An answer I read on another thread similar to this one that worked for me when I had this problem involved the layout xml.

    Your logcat says "No view found for id 0x7f080011".

    Open up the gen->package->R.java->id and then look for id 0x7f080011.

    When I had this problem, this id belonged to a FrameLayout in my activity_main.xml file.

    The FrameLayout did not have an ID (there was no statement android:id = "blablabla").

    Make sure that all of your components in all of your layouts have IDs, particularly the component cited in the logcat.

    0 讨论(0)
  • 2020-11-22 06:08

    In my case I had a SupportMapFragment in a recycler view item (I was using the lower overhead "liteMode" which makes the map appear as non-interactive, almost like a static image). I was using the correct FragmentManager, and everything appeared to work fine... with a small list. Once the list of items exceeded the screen height by a bit then I started getting this issue when scrolling.

    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.

    What I forgot to do was ensure that my code didn't run twice. I.e. in onAttachedToWindow event, check if my dynamic SupportMapFragment was still null before trying to create a new instance of it and do a Fragment replace. When the item goes off the top of the RecyclerView, it is detached from the window, then reattached when you scroll back to it, so this event is fired multiple times.

    Once I added the null check, it happened only once per RecyclerView item and issue went away! TL;DR!

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