FragmentTabHost在异步postExecute中添加选项卡

时间:2014-11-10 14:05:22

标签: android android-fragments android-tabhost android-tabs

我在尝试运行我的应用时在logCat中获取此信息:

java.lang.IllegalStateException: No tab known for tag null

我尝试做的是在onCreateView中初始化Tabhost,如下所示:

mTabHost = (FragmentTabHost) mView.findViewById(android.R.id.tabhost);

然后在onPostExecute中执行此操作:

 //setup tabhost
            mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_comminity);
            Log.v("---", "adding tabs");
            b = new Bundle();

            if (getArguments().getString(IntentConstants.KEYWORD) != null)
                if (getArguments().getString(IntentConstants.KEYWORD).length() > 0) {
                    if (key == null) {
                        key = getArguments().getString(IntentConstants.KEYWORD);
                        doTheRequest();
                    } else {
                        if (!key.equals(getArguments().getString(IntentConstants.KEYWORD)))
                            doTheRequest();
                    }

                }
            mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(getLocalizationString("lblClips")),
                    CommunityClipsFragment.class, b);

            mTabHost.addTab(mTabHost.newTabSpec("clipboards").setIndicator(getLocalizationString("lblClipboards")),
                    FragmentSearchMyClipboards.class, b);

            final int reverseTabs[] = {1, 0};
            mTabHost.getTabWidget().setDividerDrawable(R.color.color_translucent_grey);
            View v = mTabHost.getTabWidget().getChildTabViewAt(reverseTabs[mTabHost.getCurrentTab()]);
            ((TextView) v.findViewById(android.R.id.title)).setTypeface(null, Typeface.NORMAL);
            ((TextView) v.findViewById(android.R.id.title)).setTextColor(Color.parseColor("#a3a3a3"));
            v.setBackgroundResource(R.drawable.border);
            View v2 = mTabHost.getTabWidget().getChildTabViewAt(mTabHost.getCurrentTab());
            v2.setBackgroundColor(getResources().getColor(R.color.transparent));
            ((TextView) v2.findViewById(android.R.id.title)).setTextColor(getResources().getColor(R.color.color_clipixorange));
            v2.setBackgroundResource(R.drawable.border);

            mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
                @Override
                public void onTabChanged(String s) {
                    View v = mTabHost.getTabWidget().getChildTabViewAt(reverseTabs[mTabHost.getCurrentTab()]);
                    ((TextView) v.findViewById(android.R.id.title)).setTypeface(null, Typeface.NORMAL);
                    ((TextView) v.findViewById(android.R.id.title)).setTextColor(Color.parseColor("#a3a3a3"));
//                v.setBackgroundColor(getResources().getColor(R.color.transparent));
                    v.setBackgroundResource(R.drawable.border);
                    View v2 = mTabHost.getTabWidget().getChildTabViewAt(mTabHost.getCurrentTab());
//                v2.setBackgroundColor(getResources().getColor(R.color.transparent));
                    v2.setBackgroundResource(R.drawable.border);
                    ((TextView) v2.findViewById(android.R.id.title)).setTypeface(null, Typeface.BOLD);
                    ((TextView) v2.findViewById(android.R.id.title)).setTextColor(getResources().getColor(R.color.color_clipixorange));
                }
            });
            progress.setVisibility(View.GONE);

任何想法如何解决这个问题,因为我需要首先从服务器获取数据,然后将数据包数据放到子片段中。 这是我的xml:

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

    <ProgressBar
        android:id="@+id/frag_progress"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FragmentTabHost
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@android:id/tabhost">

            <LinearLayout
                android:id="@+id/LinearLayout01"
                android:orientation="vertical"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent">
                </TabWidget>

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent">
                </FrameLayout>

            </LinearLayout>

        </FragmentTabHost>


    </FrameLayout>
</FrameLayout>

和我的logCat输出:

java.lang.IllegalStateException: No tab known for tag null
            at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:330)
            at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:280)
            at android.view.View.dispatchAttachedToWindow(View.java:9888)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2299)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2307)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2307)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2307)
            at android.view.ViewGroup.addViewInner(ViewGroup.java:3480)
            at android.view.ViewGroup.addViewInLayout(ViewGroup.java:3417)
            at android.view.ViewGroup.addViewInLayout(ViewGroup.java:3398)
            at android.support.v4.view.ViewPager.addView(ViewPager.java:1307)
            at android.view.ViewGroup.addView(ViewGroup.java:3269)
            at android.view.ViewGroup.addView(ViewGroup.java:3249)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
            at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:12881)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
            at android.view.View.measure(View.java:12881)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
            at android.view.View.measure(View.java:12881)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:828)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
            at android.view.View.measure(View.java:12881)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
            at android.view.View.measure(View.java:12881)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4441)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

看起来问题是你在执行任务之前推迟调用setup()。它应该在膨胀视图后直接调用。

来自FragmentTabHost docs

  

在对层次结构进行充气后,将其置于视图层次结构中   您必须致电 setup(Context, FragmentManager, int) 才能完成   标签主机的初始化。