Android Fragment no view found for ID?

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

    If you are trying to replace a fragment within a fragment with the fragmentManager but you are not inflating the parent fragment that can cause an issue.

    In BaseFragment.java OnCreateView:

    if (savedInstanceState == null) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container, new DifferentFragment())
                        .commit();
            }
    
    return super.onCreateView(inflater, container, savedInstanceState);
    

    Replace super.onCreateView(inflater, container, savedInstanceState); with inflating the correct layout for the fragment:

            return inflater.inflate(R.layout.base_fragment, container, false);
    
    0 讨论(0)
  • 2020-11-22 06:09

    Another scenario I have met. If you use nested fragments, say a ViewPager in a Fragment with it's pages also Fragments.

    When you do Fragment transaction in the inner fragment(page of ViewPager), you will need

    FragmentManager fragmentManager = getActivity().getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    

    getActivity() is the key here. ...

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

    My mistake was on the FragamentTransaction.

    I was doing this t.replace(R.layout.mylayout); instead of t.replace(R.id.mylayout);

    The difference is that one is the layout and the other is a reference to the layout(id)

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

    I had the same issue but my issue was happenning on orientation change. None of the other solutions worked. So it turns out that I forgot to remove setRetainInstance(true); from my fragments, when doing a two or one pane layout based on screen size.

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

    This page seems to be a good central location for posting suggestions about the Fragment IllegalArgumentException. Here is one more thing you can try. This is what finally worked for me:

    I had forgotten that I had a separate layout file for landscape orientation. After I added my FrameLayout container there, too, the fragment worked.


    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>
    

    where former_fragment_layout is the name of the xml layout file that you were trying to use in your fragment. See Re-using Layouts with include for more info.

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

    This issue also happens when you don't put <include layout="@layout/your_fragment_layout"/> in your app_bar_main.xml

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