不幸的是,My Application已停止(Android Studio Emulator)

时间:2014-07-30 11:39:54

标签: java android emulation

我是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>

这是MainActivity

@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

1 个答案:

答案 0 :(得分:0)

您不应该为这样的组件定义id,因为如果您这样定义,它将在android库中搜索预定义的id。如果不存在,它将抛出空指针异常。因此,您应该定义id,例如android:id =“ @ + id / id_name”。