单击选项卡时无法启动活动

时间:2013-11-23 12:10:57

标签: android-intent android-activity android-tabhost

我正在为Android平板电脑开发应用程序。我有一个7英寸的平板电脑,在我的应用程序中,当我点击一个标签时,我想调用一个活动。当我点击一个标签时,它显示错误。你能告诉我我在代码中做错了吗?

以下是完整的源代码:

public class Home extends Activity {

    private TabHost thEvent;

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        thEvent = (TabHost) findViewById(android.R.id.tabhost);
        thEvent.setup();

        // Tab 1
        TabHost.TabSpec specs = thEvent.newTabSpec("tag1");
        specs.setContent(R.id.tab1);
        // code for calling other activity
        specs.setContent(new Intent(Home.this, PhotosActivity.class));
        specs.setIndicator("Readymade");

        thEvent.addTab(specs);

        specs = thEvent.newTabSpec("tag2");
        specs.setContent(R.id.tab2);
        specs.setIndicator("Speciality");
        thEvent.addTab(specs);

        specs = thEvent.newTabSpec("tag3");
        specs.setContent(R.id.tab3);
        specs.setIndicator("Tailormade");
        thEvent.addTab(specs);

        specs = thEvent.newTabSpec("tag4");
        specs.setContent(R.id.tab4);
        specs.setIndicator("Low Price");
        thEvent.addTab(specs);

        specs = thEvent.newTabSpec("tag5");
        specs.setContent(R.id.tab5);
        specs.setIndicator("Corporate");
        thEvent.addTab(specs);

        specs = thEvent.newTabSpec("tag6");
        specs.setContent(R.id.tab6);
        specs.setIndicator("Special Offers");
        thEvent.addTab(specs);

        specs = thEvent.newTabSpec("tag7");
        specs.setContent(R.id.tab7);
        specs.setIndicator("More");

        thEvent.addTab(specs);

    }

}

logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kesaritab2/com.example.kesaritab2.Home}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
    at android.widget.TabHost.setCurrentTab(TabHost.java:413)
    at android.widget.TabHost.addTab(TabHost.java:240)
    at com.example.kesaritab2.Home.onCreate(Home.java:33)
    at android.app.Activity.performCreate(Activity.java:5133)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    ... 11 more

1 个答案:

答案 0 :(得分:0)

According to the documentation on TabHost,当您与TabHost.setup (LocalActivityManager)一起使用时,应该致电TabHost.setContent(Intent)

  

如果您正在使用setContent(android.content.Intent),则必须调用此方法,因为启动本地活动需要activityGroup。如果您扩展TabActivity,则会为您完成此操作。

换句话说,如果您只是让您的活动扩展TabActivity,它将使您的生活变得相当容易,但请注意TabHost结构已被弃用,转而使用选项卡式操作栏模式。