应用程序在tabhost中的setContent(new Intent)上崩溃

时间:2018-12-06 19:38:30

标签: android android-studio android-intent tabs android-tabhost

我正在使用Android的Tabhost,但无法在“主要活动”中查看我的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)

1 个答案:

答案 0 :(得分:0)

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

因此,您需要告知活动有关状态的信息,然后执行以下代码:

tabHost.setup(this.getLocalActivityManager());

我还想提醒您注意,自API级别13开始,不推荐使用TabHost。TabHost的替代品是Fragment and Fragment Manager。确保您没有实现过时/过时的类是一个很好的做法。

  

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

LocalActivityManager