简体   繁体   中英

My Android TabHost Is Crashing Will NullPointer Exception

Hi I really need some help with my tabhost using android

the problem is that when I click on the list item that will launch the tab activity it crashes with no errors in the debugger or log cat. The tab host is to have control of 3 or more activities and the tab widget is nested in a horizontal scrollview ... I know it's not the scrollview causing it to crash as it was crashing before I added it.

here's my code for the tab activity:

public class BulbaTab extends TabActivity {
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tablay);

    // create the TabHost that will contain the Tabs
    tabHost = (TabHost)findViewById(android.R.id.tabhost);

    tabHost.setup();

    TabHost.TabSpec tab1 = tabHost.newTabSpec("Dex Entry");
    tab1.setIndicator("Dex Entry");
    tab1.setContent(new Intent(this,Bulbasaur.class));

    TabHost.TabSpec tab2 = tabHost.newTabSpec("Moves List");
    tab2.setIndicator("Moves");
    tab2.setContent(new Intent(this,BulbasaurMovesList.class));

    TabHost.TabSpec tab3 = tabHost.newTabSpec("Evolution & Breeding");
    tab3.setIndicator("Evolution & Breeding");
    tab3.setContent(new Intent(this,BulbasaurBreeding.class));
    // Set the Tab name and Activity
    // that will be opened when particular Tab will be selected

    /** Add the tabs  to the TabHost to display. */
    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);

}


}

And here's The Layout for the tabhost:

TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@android:id/tabhost"

  LinearLayout
        android:id="@+id/LinearLayout01"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/horizontalScrollView"
TabWidget
        android:id="@android:id/tabs"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
TabWidget
HorizontalScrollView
FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        FrameLayout
LinearLayout
TabHost

had to remove tags as it wouldn't show on the site

here's the errors showing

           java.lang.RuntimeException: Unable to start activity ComponentInfo{net.metaruptdonations.MetaDex/net.metaruptdonations.MetaDex.BulbaTab}: java.lang.RuntimeException: Unable to start activity ComponentInfo{net.metaruptdonations.MetaDex/net.metaruptdonations.MetaDex.Bulbasaur}: java.lang.NullPointerException
              at android.app.ActivityThread.performLaunchActivity  (ActivityThread.java:2305)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
              at android.app.ActivityThread.access$900(ActivityThread.java:161)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:157)
              at android.app.ActivityThread.main(ActivityThread.java:5356)
              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:1265)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
              at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{net.metaruptdonations.MetaDex/net.metaruptdonations.MetaDex.Bulbasaur}:
        java.lang.NullPointerException
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
               at android.app.ActivityThread.startActivityNow(ActivityThread.java:2140)
               at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
               at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
               at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
               at android.widget.TabHost.setCurrentTab(TabHost.java:413)
               at android.widget.TabHost.addTab(TabHost.java:240)
               at net.metaruptdonations.MetaDex.BulbaTab.onCreate(BulbaTab.java:39)
               at android.app.Activity.performCreate(Activity.java:5426)
               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
               at android.app.ActivityThread.access$900(ActivityThread.java:161)
               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
               at android.os.Handler.dispatchMessage(Handler.java:102)
               at android.os.Looper.loop(Looper.java:157)
               at android.app.ActivityThread.main(ActivityThread.java:5356)
               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:1265)
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
               at dalvik.system.NativeStart.main(Native Method)
              Caused by: java.lang.NullPointerException
               at net.metaruptdonations.MetaDex.Bulbasaur.onCreate(Bulbasaur.java:48)
        at android.app.Activity.performCreate(Activity.java:5426)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
        at android.app.ActivityThread.startActivityNow(ActivityThread.java:2140)
        at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
        at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
        at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
        at android.widget.TabHost.setCurrentTab(TabHost.java:413)
        at android.widget.TabHost.addTab(TabHost.java:240)
        at net.metaruptdonations.MetaDex.BulbaTab.onCreate(BulbaTab.java:39)
        at android.app.Activity.performCreate(Activity.java:5426)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
        at android.app.ActivityThread.access$900(ActivityThread.java:161)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)

HorizontalScrollView can have only one childview you need to change your xml like this

 <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:id="@+id/LinearLayout01"
        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" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </FrameLayout>
    </LinearLayout>

  </TabHost>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM