2012-07-04 34 views
1

好吧,我有一个继承自FragmentManager的类。在这个类里面我有一个FragmentPagerAdapter,它创建了一些片段选项卡并插入到我的TabHost中。代码编译得很好并且工作正常。我在下面放置了这些类的一些部分。我使用的是这样做的部份的Android supprt包V4FragmentManager,LocalActivityManager和TabHost.setup()

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。

+0

严,你只需要使用片段的标签,对吧?你为什么使用tabhost,意图和所有这些? – Ixx

+0

请使用'android-fragments'标签代替'fragment'请使用:) –

回答

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

如果您正在寻找标签式ViewPager,那么您应该使用Jake Wharton的['ViewPagerIndicator'](http://viewpagerindicator.com/):) –

相关问题