Android fail to use tabhost

孤者浪人 提交于 2019-12-25 02:30:06

问题


I wrote this code to have several tabs in my view. The problem is: I've only one tab because I have an exception which says:

Did you forget to call public void setup (LocalActivityManager aactivityGroup)

My class:

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

And my code to have tabs :

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);
    }
}

I saw that I can use tabHost.setup(LocalActivityManager activityGroup), so I created an object localactivitymanager but it's deprecated, and I've again only ONE tab with exception:

Activities can't be added until the containing group has been created.

So I really don't understand what I need to do someone to help me plz?


回答1:


This is viewpager. Try this.. Specify how many tabs you need in the getcount function

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

}

}

Here on change position of tabs do what you want...

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

}



来源:https://stackoverflow.com/questions/24652387/android-fail-to-use-tabhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!