Android Error : No Tab known for Null Tag

我的梦境 提交于 2019-12-12 01:59:23

问题


I need a fixed area (around 20%) at top and then 3 tabs below it. The xml is

mylist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.2"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.05"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/list_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3"
            android:textColor="@color/loclistitem_text"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/list_spec"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.1"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/list_user"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.8"
        android:orientation="horizontal" >

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.05"
            android:visibility="invisible" />

        <FrameLayout
            android:id="@+id/list_realtabcontent"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.95" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.05"
            android:visibility="invisible" />
    </LinearLayout>

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CCF8FB" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dip"
            android:layout_height="0dip"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

MyListFragment.java

    public class MyListFragment extends Fragment {

        private FragmentTabHost mTabHost;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub

            View rootView = inflater.inflate(R.layout.mylist, container,
                    false);
            TextView name = (TextView) rootView.findViewById(R.id.list_name);
            TextView spec = (TextView) rootView.findViewById(R.id.loc_spec);
            TextView users = (TextView) rootView.findViewById(R.id.list_user);
            name.setText(AppSession.getInstance().getData().getName());
            mTabHost = new FragmentTabHost(getActivity());

            mTabHost.setup(getActivity(), getChildFragmentManager(),
                    R.id.list_realtabcontent);

            Bundle arg1 = new Bundle();
            arg1.putInt("Tab1", 1);

            mTabHost.addTab(
                    mTabHost.newTabSpec("Tab1").setIndicator("Tab1",
                            getResources().getDrawable(R.drawable.tab_left)),
                    Tab1.class, arg1);

            Bundle arg2 = new Bundle();
            arg2.putInt("Tab2", 2);
            mTabHost.addTab(
                    mTabHost.newTabSpec("Tab2").setIndicator("Tab2",
                            getResources().getDrawable(R.drawable.tab_middle)),
                    Tab2.class, arg2);

            Bundle arg3 = new Bundle();
            arg3.putInt("Tab3", 3);
            mTabHost.addTab(
                    mTabHost.newTabSpec("Tab3").setIndicator("Tab3",
                            getResources().getDrawable(R.drawable.tab_rigth)),
                    Tab3Fragment.class, arg2);

            return rootView;

        }

        @Override
        public void onDetach() {
            super.onDetach();

            try {
                Field childFragmentManager = Fragment.class
                        .getDeclaredField("mChildFragmentManager");
                childFragmentManager.setAccessible(true);
                childFragmentManager.set(this, null);

            } catch (NoSuchFieldException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

    }

I get "No Tab known for Tag null" at android.support.v4.app.FragmentTabHost.doTabChanged.

The error is resolved if I change return rootView to return mTabHost. But then the fixed area at top isn't showing. Its only displaying 3 tabs at the top of the screen. I tried using Activity to set this particular screen and it worked fine(both fixed area at top and tabs are displayed correctly) but I need it using fragments as I am using Navigation Drawer so this has to be fragment. Please advice.

Modified xml

xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCF8FB" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/loc_playlist_locname"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.2" />

        <FrameLayout
            android:id="@+id/playlist_realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.1" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dip"
            android:layout_height="0dip"
            android:layout_weight="0" />
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

回答1:


I made a few changes to your code: - In implementation, use the FragmentTabHost specified in layout - Use Framelayout specified within FragmentTabHost Element in layout in TabHost setup method. It seems to work, check out code below:

XML file:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.2"
    android:orientation="vertical" >
    <View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.05"
    android:visibility="invisible" />
    <TextView
    android:id="@+id/list_name"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.3"
    android:textStyle="bold" />
    <TextView
    android:id="@+id/list_spec"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.3" />
   <View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:visibility="invisible" />
    <TextView
    android:id="@+id/list_user"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.2" />
    </LinearLayout>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.8"
    android:orientation="horizontal" >
    <View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.05"
    android:visibility="invisible" />
   <View
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="0.05"
   android:visibility="invisible" />
   </LinearLayout>
   <android.support.v4.app.FragmentTabHost
   android:id="@+id/tabhost"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#CCF8FB" >
   <FrameLayout
   android:id="@+id/list_realtabcontent"
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="0.95" />
   </android.support.v4.app.FragmentTabHost>
   </LinearLayout>

Fragment Class:

    public class MyListFragment extends Fragment {

        private FragmentTabHost mTabHost;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub

            View rootView = inflater.inflate(R.layout.activity_main, container,
                    false);
            TextView name = (TextView) rootView.findViewById(R.id.list_name);
            TextView spec = (TextView) rootView.findViewById(R.id.list_spec);
            TextView users = (TextView) rootView.findViewById(R.id.list_user);
            mTabHost = (FragmentTabHost)rootView.findViewById(R.id.tabhost);

            mTabHost.setup(getActivity(), getChildFragmentManager(),
                    R.id.list_realtabcontent);

            Bundle arg1 = new Bundle();
            arg1.putInt("Tab1", 1);

            mTabHost.addTab(
                   mTabHost.newTabSpec("Tab1").setIndicator("Tab1",
                            getResources().getDrawable(R.drawable.tab_chat_unselected)),
                      TabFragment.class, arg1);

            Bundle arg2 = new Bundle();
            arg2.putInt("Tab2", 2);
            mTabHost.addTab(
                    mTabHost.newTabSpec("Tab2").setIndicator("Tab2",
                            getResources().getDrawable(R.drawable.tab_chat_unselected)),
                    TabFragment.class, arg2);

            Bundle arg3 = new Bundle();
            arg3.putInt("Tab3", 3);
            mTabHost.addTab(
                    mTabHost.newTabSpec("Tab3").setIndicator("Tab3",
                            getResources().getDrawable(R.drawable.tab_chat_unselected)),
                    TabFragment.class, arg3);

            return rootView;

        }



回答2:


You can use this library. Having a Tabs with Navigation Drawers is the not provided directly. If u want to have tabs in fragment just like google-play music. I advice you to use this library.

Just create the navigation drawer in the same way we create and jsut properly intergrate it. Thats set very simple.

The Solution provided by @Haresh may work. But if it doesn't apply the link I have provided will definetly work. I have used that library in my project refer link1 and link2

Hope it helps




回答3:


Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.20">

    </LinearLayout>


    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.80"
        android:background="#CCF8FB" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/loc_playlist_locname"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2" />

            <FrameLayout
                android:id="@+id/playlist_realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.1" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dip"
                android:layout_height="0dip"
                android:layout_weight="0" />
        </LinearLayout>

    </android.support.v4.app.FragmentTabHost>

</LinearLayout>


来源:https://stackoverflow.com/questions/25300562/android-error-no-tab-known-for-null-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!