无法将标签添加到片段中

时间:2015-10-06 12:22:34

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

我正在尝试构建和包含2个片段的Activity。我已经构建了其中一个但我希望另一个在其中有标签。问题是我在tabHost.addTab()上收到错误;

这是我的片段

public class StatisticsFragment extends android.support.v4.app.Fragment {

Context mContext;
FragmentTabHost tabHost;
View rootView;
public StatisticsFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    mContext = getActivity().getApplicationContext();

    rootView = inflater.inflate(R.layout.fragment_statistics, container, false);

    tabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);

    tabHost.setup(getActivity(), getFragmentManager(), android.R.id.tabcontent);
Intent purposeIntent = new Intent(mContext, PurposeTab.class);
    FragmentTabHost.TabSpec tabPurpose = tabHost
            .newTabSpec("Purpose")
            .setIndicator("Purpose")
            .setContent(purposeIntent);

    Intent priorityIntent = new Intent(mContext, PriorityTab.class);
    FragmentTabHost.TabSpec tabPriority = tabHost
            .newTabSpec("Priority")
            .setIndicator("Priority")
            .setContent(priorityIntent);

    tabHost.addTab(tabPurpose);
    tabHost.addTab(tabPriority);
    tabHost.setCurrentTab(1);
    return rootView;
}

这是我的活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StatisticsFragment fragmentStatistics = new StatisticsFragment();
    MainPageButtonsFragment fragmentMain = new MainPageButtonsFragment();


    getSupportFragmentManager().beginTransaction().add(R.id.fragment_statistics, fragmentStatistics, "Statistics").commit();
    getSupportFragmentManager().beginTransaction().add(R.id.main_page_buttons_fragment, fragmentMain, "Buttons").commit();
}

这是我的LogCat,它表明我的错误在addTab上,甚至暗示我可能忘记了“设置方法”,但我不是!

E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?

10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:692)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.widget.TabHost.setCurrentTab(TabHost.java:358)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.widget.TabHost.addTab(TabHost.java:236)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at .StatisticsFragment.onCreateView(StatisticsFragment.java:64)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:511)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
10-06 12:03:56.967 4033-4033/ E/AndroidRuntime:     at android.app.Activity.performStart(Activity.java:5018)

1 个答案:

答案 0 :(得分:0)

在我看来,最简单的方法是将标签添加到任何内容(Activity,Fragment),使用来自Google Design支持库的TabLayout。

在布局文件中,添加TabLayout和ViewPager:

<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/white"
        android:gravity="bottom"
        app:tabGravity="fill"
        app:tabMode="fixed"/>
--------------------------------------
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

在您的片段/活动文件中,通过调用tabLayout.setupWithViewPager(mPager);

连接它们

您还应该在ViewPagerAdapter中覆盖您的getPageTitle方法:

public static class ViewPagerAdapter extends FragmentStatePagerAdapter {
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    private String mTabTitles[] = new String[]{"Tab 1", "Tab 2"};

    @Override
    public android.support.v4.app.Fragment getItem(int num) {
        return MyFragment.newInstance();
    }
    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return mTabTitles[position];
    }
}