android - 应用程序在 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)
解决方案
正如错误中所述:
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 兼容包在旧平台上使用。
推荐阅读
- reactjs - 单击 ReactJS 中的链接时在新窗口中打开页面
- python - 了解 Keras 层的形状
- javascript - Angular 8:同步/顺序运行组件类方法
- google-bigquery - BigQuery 无法在 SELECT * REPLACE 中使用分析函数
- r - 无序列表中的多种文本格式
- knime - 如何保存 KNIME 循环的中间结果?
- java - 为什么在堆中创建字符串对象和字符串文字的串联?
- javascript - 浏览器刷新div对齐更改后的jquery
- javascript - 在画布中绘制 5 个点的椭圆
- wordpress - 如何在woocommerce中限制每个用户每天一个订单