我已经创建了一个应用程序来显示TabHost
。但是当我在模拟器上启动应用程序时,它会说App Unfortunately Closed
。我上网搜索,但找不到任何解决方案。我的代码是 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabSpec tab3 = tabHost.newTabSpec("Third tab");
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,Tab1Activity.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,Tab2Activity.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,Tab3Activity.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
我已经为Tab1,Tab2和Tab3创建了单独的。java文件。
logcat是
06-01 01:40:35.429 1847-1847/com.realtech。要利用dalvikvm june_1_tab W/:threaddid =1:线程退出时未捕获异常(group=0xa4cd3b20)06-01 01:40:35.441 1847-1847/com.realtech。june_1_tab E/AndroidRuntime:FATAL EXCEPTION: main Process: com.realtech。june_1_tab, PID: 1847java.lang.RuntimeException:无法启动活动ComponentInfo {com.realtech.june_1_tab/com.realtech.june_1_tab.MainActivity}:java.lang.NullPointerException在android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2184)在android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2233)在android.app.ActivityThread.access$800(ActivityThread.java:135android.app.ActivityThread H.handleMessage美元(ActivityThread.java: 1196)android.os.Handler.dispatchMessage(Handler.java:102android.os.Looper.loop (Looper.java: 136)android.app.ActivityThread.main (ActivityThread.java: 5001)java.lang.reflect.Method。调用的(原生方法java.lang.reflect.Method.invoke (Method.java: 515)com.android.internal.os.ZygoteInit MethodAndArgsCaller.run美元(ZygoteInit.java: 785)(ZygoteInit.java:601dalvik.system.NativeStart。main(Native Method)原因:java.lang.NullPointerException在android.widget.TabHost.addTab (TabHost.java: 236)com.realtech.june_1_tab.MainActivity.onCreate (MainActivity.java: 34)android.app.Activity.performCreate (Activity.java: 5231)android.app.Instrumentation.callActivityOnCreate (Instrumentation.java: 1087)在android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2148)在android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2233)android.app.ActivityThread.access 800美元(ActivityThread.java: 135)
在android.app.ActivityThread H.handleMessage美元(ActivityThread.java: 1196)
android.os.Handler.dispatchMessage(Handler.java:102android.os.Looper.loop (Looper.java: 136 ) 在android.app.ActivityThread.main (ActivityThread.java: 5001)
在java.lang.reflect.Method。调用的(原生方法java.lang.reflect.Method.invoke (Method.java: 515 ) 在com.android.internal.os.ZygoteInit MethodAndArgsCaller.run美元(ZygoteInit.java: 785)com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 601)
在dalvik.system.NativeStart。main(本地方法)[06-01 01:42:24.1211847 - 1847/com.realtech。june_1_tab I/Process:发送信号。PID: 1847团体:9
你有一个错误。在布局XML文件中,你使用的tabhost id默认设置@+id/tabhost没有运行,必须更改为@android:id/tabhost.
在onCreate()中有一个空指针异常,第34行。
另外,我不推荐你正在使用的模式(活动与标签托管其他活动)。尝试用FragmentPagerAdapter和Fragments作为UI切换到ViewPager。