Android无法使用tabhost

时间:2014-07-09 11:23:13

标签: android tabs android-tabhost

我写了这段代码,在我的视图中有几个标签。问题是:我只有一个标签,因为我有一个例外:

您是否忘记致电public void setup (LocalActivityManager aactivityGroup)

我的课程:

MainActivity extends FragmentActivity (I don't use tabactivity because it's deprecated)

我的代码有标签:

try
{
    Intent intent = new Intent(this, firstActivity.class);
    tabHost = (TabHost) findViewById(R.id.tabhost);
    tabHost.setup();

    for (String tab_name : tabs) {
        tabSpec = tabHost.newTabSpec(tab_name).
        setIndicator(tab_name, getResources().getDrawable(R.drawable.test_logo))
        .setContent(intent);
        tabHost.addTab(tabSpec);
    }
}

我看到我可以使用tabHost.setup(LocalActivityManager activityGroup),所以我创建了一个对象localactivitymanager,但它已被弃用,而且我只有一个标签,但有例外:

在创建包含组之前,无法添加活动。

所以我真的不明白我需要做些什么来帮助我PLZ?

1 个答案:

答案 0 :(得分:0)

这是viewpager。试试这个.. 在getcount函数中指定所需的选项卡数

public class ParentViewPagerFragment extends Fragment {

public static final String TAG = ParentViewPagerFragment.class.getName();

public static ParentViewPagerFragment newInstance() {
    return new ParentViewPagerFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_parent_viewpager,
            container, false);


    ViewPager viewPager = (ViewPager) root.findViewById(R.id.viewPager);
    /**
     * Important: Must use the child FragmentManager or you will see side
     * effects.
     */
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

    return root;
}
public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return 7;
    }

    @Override
    public Fragment getItem(int position) {
        Bundle args = new Bundle();
        args.putInt(ChildFragment.POSITION_KEY, position);
        return ChildFragment.newInstance(args);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        position = position + 1;
        return "Lesson " + position;
    }

}

}

这里改变了标签的位置,做你想做的......

public class ChildFragment1 extends Fragment {

public static final String POSITION_KEY = "FragmentPositionKey";
private int position;
 String htmlText,link;
WebView w;
public static ChildFragment1 newInstance(Bundle args) {
    ChildFragment1 fragment = new ChildFragment1();
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    position = getArguments().getInt(POSITION_KEY);

    View root = inflater.inflate(R.layout.fragment_child, container, false);


    w = (WebView)root.findViewById(R.id.webView1);

        if(position==0)
        {
            // your code here
        }


        if(position==1)
        {
            // your code here
        }

        if(position==2)
        {
            // your code here
        }
        if(position==3)
        {
            // your code here
        }




    return root;
}

}