首页 > 解决方案 > 应用程序在 tabhost 中的 setContent(new Intent) 上崩溃

问题描述

我正在使用 Android 的 Tabhost,无法在 Main Activity 中查看我的 A 活动。它在setIndicatorsetContent(new Intent(this, a.class)). 需要进行哪些更改?我需要更改 A 活动中的一些代码吗?


MainActivity 代码

public class MainActivity extends AppCompatActivity {

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

    TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
    tabHost.setup();

    //tab 1
    TabHost.TabSpec spec  = tabHost.newTabSpec("Tab 1");
    spec.setContent(new Intent(this,a.class));
    spec.setIndicator("Exams");
    tabHost.addTab(spec);

    //tab 2
    spec = tabHost.newTabSpec("Tab 2");
    spec.setContent(new Intent(this,b.class));
    spec.setIndicator("Pratice");
    tabHost.addTab(spec);


    }
}

错误日志

Process: com.example.vivek.helloworld, PID: 17776
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vivek.helloworld/com.example.vivek.helloworld.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
  Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:722)
    at android.widget.TabHost.setCurrentTab(TabHost.java:388)
    at android.widget.TabHost.addTab(TabHost.java:222)
    at com.example.vivek.helloworld.MainActivity.onCreate(MainActivity.java:22)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)

标签: androidandroid-studioandroid-intenttabsandroid-tabhost

解决方案


正如错误中所述: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
您需要声明 localactivitymanager 以解决此错误。(仅供参考 - 当您在非法或不适当的时间调用该方法时会发生 IllegalStateException)

因此,您需要通知活动有关状态的信息,为此,请实现以下代码:

tabHost.setup(this.getLocalActivityManager());

我还想提请您注意,自 API 级别 13 以来,TabHost 已被弃用。 TabHost 的替代品是 Fragment 和 Fragment Manager。确保您没有实现过时/弃用的类是一种很好的做法。

此类在 API 级别 13 中已弃用。
请改用新的 Fragment 和 FragmentManager API;这些也可以通过 Android 兼容包在旧平台上使用。

本地活动管理器


推荐阅读