【问题标题】:Tabhost in fragment is throwing - you must specify a way to create the tab indicator exception片段中的 Tabhost 正在抛出 - 您必须指定一种方法来创建选项卡指示器异常
【发布时间】:2014-11-13 08:19:44
【问题描述】:

我正在尝试在我的片段中添加一个标签主机,它将包含 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)

这是我的片段中的重要代码,它正在执行选项卡初始化并提供选项卡功能:

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

    }

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

【问题讨论】:

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


    【解决方案1】:

    根据文档,选项卡需要三样东西: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
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多