Search code examples
androidandroid-tabhosthorizontalscrollview

HorizontalScrollabe with TabHost gives error


I have about 14 tabs in my Activity, hence I have used HorizontalScrollable to make it scrollable. However it gives NullPointerException error. I cannot figure out why.

The XML:

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

      <HorizontalScrollView 
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:fillViewport="true"
           android:scrollbars="none"> 

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </TabWidget>

       </HorizontalScrollView>

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



            <RelativeLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab4"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab5"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab6"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/tab7"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>
            </RelativeLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

Here is the code:

public class Events2 extends Activity {

private TabHost thEvent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_events2);

    thEvent = (TabHost) findViewById(R.id.tabhost);
    thEvent.setup();

    // Tab 1
    TabSpec specs = thEvent.newTabSpec("tag1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("PETROGYAN");
    thEvent.addTab(specs);

    specs = thEvent.newTabSpec("tag2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("PETROTALK");
    thEvent.addTab(specs);

    specs = thEvent.newTabSpec("tag3");
    specs.setContent(R.id.tab3);
    specs.setIndicator("PETRO CASE STUDY");
    thEvent.addTab(specs);

    specs = thEvent.newTabSpec("tag4");
    specs.setContent(R.id.tab4);
    specs.setIndicator("PETRODRAFT");
    thEvent.addTab(specs);

    specs = thEvent.newTabSpec("tag5");
    specs.setContent(R.id.tab5);
    specs.setIndicator("PETROSELL");
    thEvent.addTab(specs);

    specs = thEvent.newTabSpec("tag6");
    specs.setContent(R.id.tab6);
    specs.setIndicator("PETRO-DEBATE");
    thEvent.addTab(specs);

    specs = thEvent.newTabSpec("tag7");
    specs.setContent(R.id.tab7);
    specs.setIndicator("IDP");
    thEvent.addTab(specs);

}

}

P.S. I have shortened the code to post this. But I am using 14 tabs in my original activity

LOGCAT:

    09-20 19:36:35.461: E/AndroidRuntime(2796): FATAL EXCEPTION: main
09-20 19:36:35.461: E/AndroidRuntime(2796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rakeshsarangi.petrofiesta2013/com.rakeshsarangi.petrofiesta2013.Events2}: java.lang.NullPointerException
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.os.Looper.loop(Looper.java:137)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.app.ActivityThread.main(ActivityThread.java:4340)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at dalvik.system.NativeStart.main(Native Method)
09-20 19:36:35.461: E/AndroidRuntime(2796): Caused by: java.lang.NullPointerException
09-20 19:36:35.461: E/AndroidRuntime(2796):     at com.rakeshsarangi.petrofiesta2013.Events2.onCreate(Events2.java:22)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.app.Activity.performCreate(Activity.java:4465)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-20 19:36:35.461: E/AndroidRuntime(2796):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-20 19:36:35.461: E/AndroidRuntime(2796):     ... 11 more

Solution

  • // try this 
    1. when u define id xml this way android:id="@android:id/tabhost" then u get this in activity like findViewById(android.R.id.tabhost);
    2. when u define id xml this way android:id="@+id/tab1" then u get this in activity like findViewById(R.id.tab1);
    
    // in ur case try this
    
    <?xml version="1.0" encoding="utf-8"?>
    <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:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
            <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fillViewport="true"
                    android:scrollbars="none">
    
                <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >
                </TabWidget>
    
            </HorizontalScrollView>
    
            <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
    
                <RelativeLayout
                        android:id="@+id/tab1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                </RelativeLayout>
    
                <RelativeLayout
                        android:id="@+id/tab2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                </RelativeLayout>
    
                <RelativeLayout
                        android:id="@+id/tab3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                </RelativeLayout>
    
                <RelativeLayout
                        android:id="@+id/tab4"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                </RelativeLayout>
    
                <RelativeLayout
                        android:id="@+id/tab5"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                </RelativeLayout>
    
                <RelativeLayout
                        android:id="@+id/tab6"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                </RelativeLayout>
    
                <RelativeLayout
                        android:id="@+id/tab7"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                </RelativeLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
    
    
    private TabHost thEvent;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            thEvent = (TabHost) findViewById(android.R.id.tabhost);
            thEvent.setup();
    
            // Tab 1
            TabHost.TabSpec specs = thEvent.newTabSpec("tag1");
            specs.setContent(R.id.tab1);
            specs.setIndicator("PETROGYAN");
            thEvent.addTab(specs);
    
            specs = thEvent.newTabSpec("tag2");
            specs.setContent(R.id.tab2);
            specs.setIndicator("PETROTALK");
            thEvent.addTab(specs);
    
            specs = thEvent.newTabSpec("tag3");
            specs.setContent(R.id.tab3);
            specs.setIndicator("PETRO CASE STUDY");
            thEvent.addTab(specs);
    
            specs = thEvent.newTabSpec("tag4");
            specs.setContent(R.id.tab4);
            specs.setIndicator("PETRODRAFT");
            thEvent.addTab(specs);
    
            specs = thEvent.newTabSpec("tag5");
            specs.setContent(R.id.tab5);
            specs.setIndicator("PETROSELL");
            thEvent.addTab(specs);
    
            specs = thEvent.newTabSpec("tag6");
            specs.setContent(R.id.tab6);
            specs.setIndicator("PETRO-DEBATE");
            thEvent.addTab(specs);
    
            specs = thEvent.newTabSpec("tag7");
            specs.setContent(R.id.tab7);
            specs.setIndicator("IDP");
            thEvent.addTab(specs);
        }
    

    enter image description here