有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

fragment中的java Tabhost正在抛出,您必须指定创建选项卡指示符异常的方法

我试图在片段中添加一个tabhost,它将容纳2个子片段。我需要为它们添加参数,但似乎我不能这样做,因为我的日志中有这样的内容:

java.lang.IllegalArgumentException: you must specify a way to create the tab indicator.
            at 安卓.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)

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

    private void initTabs(View mView) {
            Bundle args = new Bundle();
            mTabHost = (TabHost) mView.findViewById(安卓.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 安卓.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) 个答案

  1. # 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
        ...
    }