我正在使用Android应用程序,我想使用4个选项卡进行导航,每个选项卡都使用Activites . 在这里我添加了我尝试过的代码
这是我的TabHostActivity
public class TabHostActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_host);
context = getApplicationContext();
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
tabHost.setup(mLocalActivityManager);
TabHost.TabSpec tag4= tabHost.newTabSpec(TAB_4_TAG);
TabHost.TabSpec tag3= tabHost.newTabSpec(TAB_3_TAG);
TabHost.TabSpec tag2= tabHost.newTabSpec(TAB_2_TAG);
TabHost.TabSpec tag1= tabHost.newTabSpec(TAB_1_TAG);
tag1.setIndicator("AboutCollege", getResources().getDrawable(R.drawable.college)).setContent(new Intent(this, AboutCollegeActivity.class));
tag2.setIndicator("Focus of Course", getResources().getDrawable(R.drawable.course)).setContent(new Intent(this, AboutCollegeActivity.class));
tag3.setIndicator("Admision", getResources().getDrawable(R.drawable.admission)).setContent(new Intent(this, AboutCollegeActivity.class));
tag4.setIndicator("Contact Details", getResources().getDrawable(R.drawable.contact)).setContent(new Intent(this, AboutCollegeActivity.class));
tabHost.addTab(tag1);
tabHost.addTab(tag2);
tabHost.addTab(tag3);
tabHost.addTab(tag4);
}
}
这是我的tabHost.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
这是错误日志
15:41.601 12232-12232 / com.after2.svirtzone.after2_gradle E / AndroidRuntime:FATAL EXCEPTION:main java.lang.RuntimeException:无法启动活动ComponentInfo {com.after2.svirtzone.after2_gradle / com.after2.svirtzone.after2_gradle .TabHostActivity}:java.lang.IllegalStateException:在创建包含组之前,无法添加活动 . 在Android.app.A活动中的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)以及android.app.A活动中的android.app.A活动时,我发现Android.app.A活动 . 事件(ActivityThread.java:1516) .ActivityThread $ H.handleMessage(ActivityThread.java:1296)在android.os.Handler.dispatchMessage(Handler.java:99)在android.os.Looper.loop(Looper.java:176)在android.app.ActivityThread . main(ActivityThread.java:5365)java.lang.reflect.Method.invokeNative(Native Method),位于com.android.internal.os.ZygoteInit $的java.lang.reflect.Method.invoke(Method.java:511)位于dalvik.system.NativeStart.main(本地方法)的com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)中的MethodAndArgsCaller.run(ZygoteInit.java:1102)引起:java.lang.IllegalStateException:在创建包含组之前,无法添加活动 . 在android.app.LartActivityManager.startActivity(LocalActivityManager.java:262)的android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:820)在Android的android.widget.TabHost.setCurrentTab(TabHost.java:484) . widget.TabHost.addTab(TabHost.java:286)位于android.app的android.app.Activity.performCreate(Activity.java:5326)的com.after2.svirtzone.after2_gradle.TabHostActivity.onCreate(TabHostActivity.java:70) .Instrumentation.callActivityOnCreate(Instrumentation.java:1097)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)android.app.ActivityThread.access $ 700 (ActivityThread.java:158)在android.app.A.运行时,Android.O.Doper.loop(Looper . )的android.app.A.运行时,运行android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1296),处于android.os.Handler.dispatchMessage(Handler.java:99) . java:176)在android.app.ActivityThread.main(ActivityThread.java:5365)java.lang.reflect.Method.invokeNative(Native Meth) OD)在java.lang.reflect.Method.invoke(Method.java:511)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)在com.android.internal.os.ZygoteInit .main(ZygoteInit.java:869)at dalvik.system.NativeStart.main(Native Method)
当我从另一个活动中单击该按钮时,它将移动到此tabhost . 在这里,我得到了错误日志中的异常 . 我已经搜索过,但遗憾的是我没有得到明确的答案 . 请帮我解决这个问题
1 回答
希望这有助于你
此类在API级别13中已弃用 . 新应用程序应使用Fragments而不是此类;要继续在旧设备上运行,您可以使用v4支持库,该库提供与DONUT兼容的Fragment API版本 .
http://developer.android.com/reference/android/app/TabActivity.html