FragmentManager,LocalActivityManager和TabHost.setup()

时间:2012-07-04 16:07:36

标签: android android-fragments android-fragmentactivity

好的,我有一个继承自FragmentManager的类。在这个类中,我有一个FragmentPagerAdapter,可以创建一些片段选项卡并插入我的TabHost中。代码编译精细并且有效。我把下面的部分课程放在下面。我正在使用android supprt package v4

public class TabMenu extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener{
...
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs_container);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setup();

    viewPager = (ViewPager)findViewById(R.id.viewpager);

    ...

    menuAdapter = new MenuPagerAdapter(this, tabHost, viewPager);
    menuAdapter.addTab("meutime", "Meu Time", MeuTime.class, extras);
    ...
    tabHost.setOnTabChangedListener(this);
    viewPager.setOnPageChangeListener(this);
            ...

public class MenuPagerAdapter extends FragmentPagerAdapter{
    public MenuPagerAdapter(FragmentActivity fragmentActivity, TabHost tabHost, ViewPager viewPager) {
        ...
    }

    @Override
    public Fragment getItem(int position) {
        String currentFragment = tabNames.get(position);
        return fragmentManager.findFragmentByTag(currentFragment);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return tabNames.size();
    }

    public void addTab(String tag, String tabLabel, Class<?> cls, Bundle bundle){
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(createTab(tabLabel));
        Intent intent = new Intent();
        intent.setClass(context, cls);
        intent.putExtras(bundle);
        tabSpec.setContent(intent);
        tabNames.add(tag);
        tabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    private View createTab(String tabLabel){
        View view = LayoutInflater.from(context).inflate(R.layout.tab_spec_layout, null, false);
        ...
        return view;
    }
}

当我实例化此FragmentActivity时,我收到以下异常:

java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?

在Android开发者网站上阅读不推荐使用LocalActivityManager。但它在TabHost.setup(LocalActivityManager)中使用。 如果不推荐使用此类,是否有针对此案例的解决方案?我不能使用TabContentFactory。

1 个答案:

答案 0 :(得分:0)

Unhapilly我使用过TabContentFactory。就个人而言,我不喜欢这个解决方案来创建一个空视图但是有效。

public void addTab(String tag, final String tabLabel, Class<?> cls, Bundle bundle){
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(createTab(tabLabel));
        tabSpec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                View view = new View(context);
                view.setMinimumHeight(0);
                view.setMinimumWidth(0);
                return view;
            }
        });

        fragContentInstances.add(new FragContentInfos(cls, bundle));
        tabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    private class FragContentInfos{
        private Class<?> cls;
        private Bundle bundle;
        public FragContentInfos(Class<?> cls, Bundle bundle){
            this.cls = cls;
            this.bundle = bundle;
        }
    }