进入我的活动时出现Tabhost错误

时间:2015-02-06 06:45:58

标签: android android-tabhost

当我尝试在我的活动中获取标签主机时,它显示错误,您忘记拨打public void setup(LocalActivityManager activityGroup)

public class MainActivity extends FragmentActivity {
    private Resources res;
    private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // tabHost = getTabHost();
        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);

            tabHost.setup();

        res = getResources();

        Intent intentContact = new Intent().setClass(this,
                tabOne_Activity.class);
        TabSpec tabSpecContact = tabHost.newTabSpec("Chat")
                .setIndicator("", res.getDrawable(R.drawable.ic_launcher))
                .setContent(intentContact);
        tabHost.addTab(tabSpecContact);

        Intent intentChat = new Intent().setClass(this, tabTwo_Activity.class);
        TabSpec tabSpecChat = tabHost.newTabSpec("Chat")
                .setIndicator("", res.getDrawable(R.drawable.ic_launcher))
                .setContent(intentChat);
        tabHost.addTab(tabSpecChat);

        Intent intentProfile = new Intent().setClass(this,
                tabThree_Activity.class);
        TabSpec tabSpecProfile = tabHost.newTabSpec("Chat")
                .setIndicator("", res.getDrawable(R.drawable.ic_launcher))
                .setContent(intentProfile);
        tabHost.addTab(tabSpecProfile);

        tabHost.setCurrentTab(2);


    }
}

1 个答案:

答案 0 :(得分:0)

您正尝试使用tabhost。为此,您必须扩展 TabActivity

OR

在onCreate()中编写以下代码:

LocalActivityManager mlam = new LocalActivityManager(this, false);
        mlam.dispatchCreate(savedInstanceState);
        // tabHost = getTabHost();
        //tabHost.setup();
        tabHost.setup(mlam);