0

I have created an app to show TabHost. But when i launch the application on Emulator it closes by saying App Unfortunately Closed. I searched internet but can't find any solution. My code is 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);
}

I have created separate .java files for Tab1,Tab2 and Tab3.

logcat is

06-01 01:40:35.429 1847-1847/com.realtech.june_1_tab W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (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: 1847 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.realtech.june_1_tab/com.realtech.june_1_tab.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 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:5001) 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:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.widget.TabHost.addTab(TabHost.java:236) at com.realtech.june_1_tab.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:2148) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 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:5001)
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:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method) 06-01 01:42:24.121 1847-1847/com.realtech.june_1_tab I/Process﹕ Sending signal. PID: 1847 SIG: 9

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46

2 Answers2

1

You have one mistake. In the layout XML file, the tabhost id you use default setting @+id/tabHost which is not run, have to change to @android:id/tabhost.

Cuong Phan
  • 311
  • 1
  • 4
  • 8
0

You have a null pointer exception in onCreate(), line 34.

Plus, I wouldn't recommend the pattern you are using (Activity with Tabs hosting other Activities. Try switching to a ViewPager with a FragmentPagerAdapter and Fragments as UI. It's much easier and cleaner.

Chisko
  • 3,092
  • 6
  • 27
  • 45