片段中的Tabhost正在抛出 - 您必须指定一种创建制表符指示符异常的方法

时间:2014-11-13 08:19:45

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

我正在尝试在我的片段中添加一个tabhost,它将容纳2个子片段。我需要为它们添加参数,但似乎我不能这样做,因为我在logCat中得到了这个:

java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
            at android.widget.TabHost.addTab(TabHost.java:221)
            at com.myapp.fragments.CommunitySearchFragment.addTab(CommunitySearchFragment.java:247)
            at com.myapp.fragments.CommunitySearchFragment.initTabs(CommunitySearchFragment.java:151)
            at com.myapp.fragments.CommunitySearchFragment.onCreateView(CommunitySearchFragment.java:223)

这是我的片段中的重要代码,它正在执行制表符init并提供制表符功能:

    private void initTabs(View mView) {
            Bundle args = new Bundle();
            mTabHost = (TabHost) mView.findViewById(android.R.id.tabhost);
            mTabHost.setup();
            TabInfo tabInfo = null;
            args.putSerializable("communityClipboards", rArray);
            addTab(mTabHost, mTabHost.newTabSpec("Tab1"), (tabInfo = new TabInfo("Tab1", CommunityClipsFragment.class, args)));

            args.putParcelableArrayList("communityClips", clipsArray);
            addTab(mTabHost, mTabHost.newTabSpec("Tab2"), (tabInfo = new TabInfo("Tab2", FragmentSearchMyClipboards.class, args)));

            // ------------------------
            mapTabInfo.put(tabInfo.tag, tabInfo);
            // Default to first tab
            onTabChanged("Tab1");
            //
            //
            mTabHost.setOnTabChangedListener(this);

        }

 private void addTab(TabHost tabHost,
                        TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        // Attach a Tab view factory to the spec
        tabSpec.setContent(new TabFactory(getActivity()));
        String tag = tabSpec.getTag();

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state. If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        tabInfo.fragment = getActivity().getSupportFragmentManager()
                .findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = getActivity().getSupportFragmentManager()
                    .beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            getActivity().getSupportFragmentManager().executePendingTransactions();
        }
        tabHost.addTab(tabSpec);
    }

    @Override
    public void onTabChanged(String tag) {
        TabInfo newTab = (TabInfo) mapTabInfo.get(tag);
        if (mLastTab != newTab) {
            FragmentTransaction ft = getActivity().getSupportFragmentManager()
                    .beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(getActivity(),
                            newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            getActivity().getSupportFragmentManager().executePendingTransactions();
        }
    }

    class TabInfo {
        private String tag;
        @SuppressWarnings("rawtypes")
        private Class clss;
        private Bundle args;
        private Fragment fragment;

        @SuppressWarnings("rawtypes")
        TabInfo(String tag, Class clazz, Bundle args) {
            this.tag = tag;
            this.clss = clazz;
            this.args = args;
        }

    }

    class TabFactory implements TabHost.TabContentFactory {

        private final Context mContext;

        /**
         * @param context
         */
        public TabFactory(Context context) {
            mContext = context;
        }

        /**
         * (non-Javadoc)
         *
         * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
         */
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }

    }

那么有人可以帮我制作这段代码吗?

1 个答案:

答案 0 :(得分:0)

根据文档,标签需要三件事:1)指示符(标签或标签+图标),2)内容,3)标签。您正在添加内容和标记,但不是指标。您对newTabSpec的调用似乎是设置标签,但实际上只是设置标签的标签。

以下代码可以使用标签作为标签,但当然您可能会考虑不同的标签:

private void addTab(TabHost tabHost,
                    TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    // Attach a Tab view factory to the spec
    tabSpec.setContent(new TabFactory(getActivity()));
    String tag = tabSpec.getTag();
    tabSpec.setIndicator(tag); // you may want a different label
    ...
}