Android FragmentTabHost : No tab known for tag null

前端 未结 7 472
悲&欢浪女
悲&欢浪女 2020-12-09 01:16

I used below code and it is not render graphical layout. display error as Exception raised during rendering: No tab known for tag null.

how can i solve

7条回答
  •  生来不讨喜
    2020-12-09 01:45

    My develop environment is Android Studio 0.8.9 with API19 SDK.

    If I put the FragmentTabHost in a FragmentActivity, it works. When I put the FragmentTabHost in a Fragment, it gets "no tab known for tag null" when render and get runtime error when LayoutInflater inflate the layout.

    Thank for user3216049's answer, it's a good workaround. Sorry, I cannot vote since I am a newbie. :(

    However it display nothing in my test tab fragments. I did a small modification.

    • test_fragment.xml

    
    
    
    
    
    
    
    
    
    

    • fragment_section_dummy.xml

    
    

    • Java code The point is that I change the id to the "R.id.realtabcontent" in FragmentTabHost.setup()

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTabHost;
    
    public class TestFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, 
                    ViewGroup container, Bundle savedInstanceState) {
    
            FragmentTabHost tabHost = new FragmentTabHost(getActivity());
            inflater.inflate(R.layout.test_fragment, tabHost);
            tabHost.setup(getActivity(), 
                          getChildFragmentManager(), R.id.realtabcontent);
    
            tabHost.addTab(tabHost.newTabSpec("simple")
                .setIndicator("Simple"), DummySectionFragment.class, null);
            tabHost.addTab(tabHost.newTabSpec("contacts")
                .setIndicator("Contacts"), DummySectionFragment.class, null);
            return tabHost;
        }
    
        /**
         * A dummy fragment representing a section of the app, 
         * but that simply displays dummy text.
         */
        public static class DummySectionFragment extends Fragment {
            private static int count = 0;
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_section_dummy, 
                       container, false);
                ((TextView) rootView.findViewById(android.R.id.text1))
                        .setText("Dummy Section " + count++);
                return rootView;
            }
        }
    }
    

提交回复
热议问题