【问题标题】:Android Error : No Tab known for Null TagAndroid 错误:没有已知为 Null 标记的选项卡
【发布时间】:2014-08-14 05:16:44
【问题描述】:

我需要在顶部有一个固定区域(大约 20%),然后在其下方 3 个选项卡。 xml是

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);
            }
        }

    }

我在 android.support.v4.app.FragmentTabHost.doTabChanged 收到“No Tab known for Tag null”。

如果我将return rootView 更改为return mTabHost,错误就会得到解决。但是顶部的固定区域没有显示。它仅在屏幕顶部显示 3 个选项卡。 我尝试使用 Activity 来设置这个特定的屏幕,它工作正常(顶部的固定区域和选项卡都正确显示)但我需要使用片段,因为我正在使用导航抽屉,所以这必须是片段。请指教。

修改后的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>

【问题讨论】:

  • 在这里查看我的答案:stackoverflow.com/questions/24485468/…
  • 感谢您的链接。按照建议进行了尝试,但没有按预期工作。我需要布局中间的标签(从顶部向下大约 20-25%)。根据您的建议更改了 xml,但标签仍然排列在顶部。
  • 能否请您发布更新的 xml,然后我尝试解决您的问题。
  • 完成。添加了更改后的 xml。

标签: android android-fragments android-tabhost android-nested-fragment


【解决方案1】:

我对您的代码进行了一些更改: - 在实现中,使用布局中指定的 FragmentTabHost - 在 TabHost 设置方法的布局中使用 FragmentTabHost 元素中指定的 Framelayout。它似乎有效,请查看以下代码:

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: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>

片段类:

    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】:

您可以使用this 库。没有直接提供带有导航抽屉的选项卡。如果你想在片段中有标签,就像谷歌播放音乐一样。我建议你使用这个库。

只需以与我们创建相同的方式创建导航抽屉并正确集成它。设置很简单。

@Haresh 提供的解决方案可能有效。但是,如果它不应用我提供的链接肯定会起作用。我在我的项目中使用了该库,请参阅 link1link2

希望对你有帮助

【讨论】:

  • 非常感谢 James 提供的链接。但这不是我要找的。这些链接解释了滑动标签功能。我的标签有列表视图,每个列表项都可以点击。所以我不是在寻找滑动标签功能,而是我只需要将我的标签重新定位在布局的中间。我已经能够使用 Activity 实现这一点,但现在需要扩展片段。
【解决方案3】:

试试这个方法,希望能帮助你解决问题。

<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>

【讨论】:

  • 感谢所有帮助。按照上面的建议进行了尝试。不幸的是同样的问题。默认情况下,选项卡仍位于顶部。
  • 您尝试过使用此代码吗? bcoz 我试过了,它会从顶部放置 20% 的空间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
相关资源
最近更新 更多