设置FragmentTabHost时出错:没有标签为null的标签 - Android

时间:2014-06-05 01:42:48

标签: java android fragment-tab-host

尝试设置FragmentTabHost时出现以下错误:

06-05 03:28:04.421: E/AndroidRuntime(7860): FATAL EXCEPTION: main
06-05 03:28:04.421: E/AndroidRuntime(7860): java.lang.IllegalStateException: No tab known for tag null
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:330)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:280)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.View.dispatchAttachedToWindow(View.java:11937)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2415)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2422)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2422)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.ViewGroup.addView(ViewGroup.java:3210)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.view.ViewPager.addView(ViewPager.java:1304)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.ViewGroup.addView(ViewGroup.java:3155)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.ViewGroup.addView(ViewGroup.java:3131)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:949)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at com.devyanlab.qoment.activities.MainActivity.onTabSelected(MainActivity.java:98)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.support.v7.app.ActionBarImplICS$TabWrapper.onTabSelected(ActionBarImplICS.java:558)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1072)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:519)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.View.performClick(View.java:4204)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.view.View$PerformClick.run(View.java:17355)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.os.Handler.handleCallback(Handler.java:725)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.os.Looper.loop(Looper.java:137)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at java.lang.reflect.Method.invoke(Method.java:511)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-05 03:28:04.421: E/AndroidRuntime(7860):     at dalvik.system.NativeStart.main(Native Method)

布局:

<android.support.v4.app.FragmentTabHost
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

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

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

片段:

public class UserFragment extends android.support.v4.app.Fragment implements OnRefreshListener {

@InjectView(android.R.id.tabhost) FragmentTabHost tabhost;

...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View fragmentView = inflater.inflate(R.layout.fragment_user, container, false);

    ButterKnife.inject(this, fragmentView);

    tabhost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

    return fragmentView;
}

...

工作正常,直到突然停止工作。 我在互联网上搜索了所有答案并尝试了一切,但仍然没有运气。这让我疯了!

有什么难事吗?

2 个答案:

答案 0 :(得分:0)

尝试更改布局

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

 <FrameLayout
    android:id="@+id/realtabcontent"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" />

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


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

</LinearLayout>

答案 1 :(得分:0)

如错误所示

  

Java:非法状态异常:没有标签知道标记为null

您尝试初始化TabHOst,但TabHost为空。

尝试使用我用来初始化TabHost的代码,它运行正常。

让您的代码保持在onCreateView()。当您尝试在onViewCreated()中设置FragmentTabHost时会出现此问题。尝试在onCreateView()中设置它,并在返回视图对象之前添加至少一个选项卡。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        // Create FragmentTabHost
        mTabHost = new FragmentTabHost(getActivity());
        // Locate fragment1.xml to create FragmentTabHost
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment1);
        mTabHost.addTab(mTabHost.newTabSpec("groups").setIndicator("",getResources().getDrawable(R.drawable.tab_group_icon)),FragmentTab1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("",getResources().getDrawable(R.drawable.tab_user_icon)),FragmentTab2.class, null);


        MainActivity.fabButton.setVisibility(View.VISIBLE);
        return mTabHost;
}