0

我的活动中有大约 14 个选项卡,因此我习惯于HorizontalScrollable使其可滚动。但是它给出了NullPointerException错误。我不知道为什么。

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>

这是代码:

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);

}

}

PS我已经缩短了发布这个的代码。但我在原始活动中使用了 14 个标签

日志猫:

    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
4

2 回答 2

1
// 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);
    }

在此处输入图像描述

于 2013-10-07T04:50:50.850 回答
0

尝试这个..

只需替换以下行

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

作为

thEvent = getTabHost();

或者

thEvent = (TabHost)findViewById(android.R.id.tabhost);
于 2013-10-07T04:54:00.117 回答