【发布时间】:2014-09-22 00:58:33
【问题描述】:
我是 Android 编程新手,但我有一些 Java 经验,但我的模拟器有问题。我在装有 OS X 10.9.3 的 Macbook 上的 AndroidStudio 中使用 Nexus 5 模拟器
我试图自己解决这个问题,但我没有运气。我也搜索了这个问题,但我的问题没有解决方案。
这是我的代码:(在 activity_main 中)
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
这是主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third");
TabHost.TabSpec tab4 = tabHost.newTabSpec("Fourth");
TabHost.TabSpec tab5 = tabHost.newTabSpec("Fifth");
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this, Friends.class));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this, Map.class));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this, Camera.class));
tab4.setIndicator("Tab4");
tab4.setContent(new Intent(this, Map.class));
tab5.setIndicator("Tab5");
tab5.setContent(new Intent(this, Profile.class));
tabHost.addTab(tab1); //editor: This should be the line that throws exception
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.addTab(tab4);
tabHost.addTab(tab5);
}
最后但并非最不重要的是我的日志猫:
07-30 07:16:55.590 829-829/com.SimonLindmayr.myapplication5.app D/AndroidRuntime﹕ Shutting down VM
07-30 07:16:55.590 829-829/com.SimonLindmayr.myapplication5.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a90ba8)
07-30 07:16:55.610 829-829/com.SimonLindmayr.myapplication5.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.SimonLindmayr.myapplication5.app, PID: 829
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SimonLindmayr.myapplication5.app/com.SimonLindmayr.myapplication5.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.TabHost.addTab(TabHost.java:236)
at com.SimonLindmayr.myapplication5.app.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
07-30 07:21:56.244 829-829/com.SimonLindmayr.myapplication5.app I/Process﹕ Sending signal. PID: 829 SIG: 9
【问题讨论】:
-
你可以标记
com.SimonLindmayr.myapplication5.app.MainActivity.onCreate(MainActivity.java:34)的行 -
你确定 (TabHost)findViewById(android.R.id.tabhost);不返回 null
-
试试这个........ TabHost tabHost = getTabHost(); ---- 但请确保您的布局 (activity_main.xml) 包含 tabhost..
-
@stealthjong 如果是这样,那么它会在
tabHost.newTabSpec("First");上引发异常