FragmentTabHost add tabs in async postExecute

177 Views Asked by At

I'm getting this in logCat when trying to run my app:

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

What Im trying to do is to initialize the Tabhost in onCreateView like this:

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

and then in onPostExecute im doing this:

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

any idea how can I fix this, because I need to first fetch the data from a server and then put the bundle data to the child fragments. Here is my 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>

and my logCat output:

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

There are 1 best solutions below

1
On

It looks like the problem is that you are delaying your call to setup() until after the task is executed. It should be called directly after inflating the view.

From the FragmentTabHost docs:

When placing this in a view hierarchy, after inflating the hierarchy you must call setup(Context, FragmentManager, int) to complete the initialization of the tab host.