Android Fragment no view found for ID?

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

    This happens when you are calling from a fragment inside another one.

    use :

    getActivity().getSupportFragmentManager().beginTransaction();
    
    0 讨论(0)
  • 2020-11-22 06:11

    I encountered this problem when I tried to replace view with my fragment in onCreateView(). Like this:

    public class MyProjectListFrag extends Fragment {
    
    
        private MyProjectListFragment myProjectListFragment;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            FragmentManager mFragmentManager = getFragmentManager();
            myProjectListFragment = new MyProjectListFragment();
            mFragmentManager
                    .beginTransaction()
                    .replace(R.id.container_for_my_pro_list,
                            myProjectListFragment, "myProjectListFragment")
                    .commit();
        }
    

    It told me

    11-25 14:06:04.848: E/AndroidRuntime(26040): java.lang.IllegalArgumentException: No view found for id 0x7f05003f (com.example.myays:id/container_for_my_pro_list) for fragment MyProjectListFragment{41692f40 #2 id=0x7f05003f myProjectListFragment}
    

    Then I fixed this issue with putting replace into onActivityCreated(). Like this:

    public class MyProjectListFrag extends Fragment {
    
        private final static String TAG = "lch";
    
        private MyProjectListFragment myProjectListFragment;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            return inflater
                    .inflate(R.layout.frag_my_project_list, container, false);
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);
    
            FragmentManager mFragmentManager = getFragmentManager();
            myProjectListFragment = new MyProjectListFragment();
            mFragmentManager
                    .beginTransaction()
                    .replace(R.id.container_for_my_pro_list,
                            myProjectListFragment, "myProjectListFragment")
                    .commit();
    
        }
    
    1. You have to return a view in onCreateView() so that you can replace it later
    2. You can put any operation towards this view in the following function in fragment liftcycle, like onActivityCreated()

    Hope this helps!

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

    With Nested fragments

    For me by using getChildFragmentManager() instead of getActivity().getSupportFragmentManager() resolved crash

    java.lang.IllegalArgumentException: No view found for id

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

    The solution was to use getChildFragmentManager()

    instead of getFragmentManager()

    when calling from a fragment. If you are calling the method from an activity, then use getFragmentManager().

    That will solve the problem.

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

    I've had the same problem when was doing fragment transaction while activity creation.

    The core problem is what Nick has already pointed out - view tree has not been inflated yet. But his solution didn't work - the same exception in onResume, onPostCreate etc.

    The solution is to add callback to container fragment to signal when it's ready:

    public class MyContainerFragment extends Fragment {
        public static interface Callbacks {
            void onMyContainerAttached();
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            Log.d(TAG, "--- onAttach");
            ((Callbacks) activity).onMyContainerAttached();
        }
    
        //... rest of code
    }
    

    And then in activity:

    public class MainActivity extends Activity
            implements MyContainerFragment.Callbacks
    {
        @Override
        public void onMyContainerAttached() {
            getFragmentManager()
                    .beginTransaction()
                    .replace(R.id.containerFrame, new MyFragment())
                    .commit();
        }
    
        //...
    }
    
    0 讨论(0)
  • 2020-11-22 06:16

    I had this problem (when building my UI in code) and it was caused by my ViewPager (that showed Fragments) not having an ID set, so I simply used pager.setID(id) and then it worked.

    This page helped me figure that out.

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