0

この質問は本当に奇妙で、私を夢中にさせます。nullPointerException を取り除くことはできません。私のタスクは非常に単純です。タブホストでテストを実行するだけです。しかし、今では 1 つのタブホストを作成することさえできません! タブホストを作成するためには、注意が必要な点がいくつかあります。最初に、ID が「@android:id/tabs」のタブウィジェットと、ID が「@android:id/tabconent」のフレームレイアウトが必要です。私は注意を払うことを誓います。以下のコードでそれを見ることができます。そして、TabActivity の使用を推奨する記事を読みました。残念ながら、このタブホストはアクティビティのコンポーネントになります。そのため、通常のアクティビティで作成する必要があります。

コードが「addSpec()」に進むと、nullPointerException がスローされます。Logcat で tabHost と Host.Spec の両方を調べます。どちらも null ではありません。印刷結果をこの質問の最後に追加します。

これが私のJavaコードです。

public class Cao extends Activity {
ScrollView menu_scroll;
TabHost menu_host;
TabWidget menu_tags;
int[] contents;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testtab);
    menu_host = (TabHost) findViewById(R.id.tabhost);
    Log.e("tabhost", (menu_host == null) + "");
    menu_tags = (TabWidget) findViewById(android.R.id.tabs);
    contents = new int[] { R.id.b1, R.id.b2 };
    int count = 6;
    for (int i = 0; i < count; i++) {
        TabSpec spec = menu_host.newTabSpec(i + "");
        spec.setIndicator("no " + i);
        Log.e("spec", (spec == null) + "");
        spec.setContent(R.id.b1); **// the problem occurs at this line**
        menu_host.addTab(spec);
    }
}
}

私のxmlコードは次のとおりです

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="vertical" >

        <HorizontalScrollView
            android:id="@+id/menu_scroll"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:scrollbars="none" >

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

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

        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

    </FrameLayout>
</TabHost>

</LinearLayout>

さらに、logcat の出力

tabhost(490): false
spec(490): false
E/AndroidRuntime(490): Uncaught handler: thread main exiting due to uncaught exception  
E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tim.wirelessorder/com.tim.wirelessorder.ui.Cao}: java.lang.NullPointerException   
E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(490):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(490):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(490):  at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(490):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(490):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(490):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(490): Caused by: java.lang.NullPointerException    
E/AndroidRuntime(490):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:583)
E/AndroidRuntime(490):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:578)
E/AndroidRuntime(490):  at android.widget.TabHost$TabSpec.setContent(TabHost.java:435)
E/AndroidRuntime(490):  at com.tim.wirelessorder.ui.Cao.onCreate(Cao.java:38)
E/AndroidRuntime(490):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
4

3 に答える 3

1

//タブホストIDをに変更します

<TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

//TabActivityを次のように拡張するアクティビティ

public class HelloTabWidget extends TabActivity {

//onCreateでこれを行います

 TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

これをチェックして:

于 2012-05-23T12:46:35.330 に答える
1

単純なアクティビティの試行ではなく、TabActivity を拡張する必要があります

public class Cao extends TabActivity 

代わりに

public class Cao extends Activity 

これを変更します

menu_host = (TabHost) findViewById(R.id.tabhost);

menu_host =getTabHost(); に と

 android:id="@+id/tabhost"

android:id="@android:id/tabhost"
于 2012-05-23T12:16:38.967 に答える
0

TabHost.xml でID を宣言する方法を変更するだけで済みました。を使用するのではなく、"@+id/tabhost"使用し"@android:id/tabhost"ました。TabHost次に、コードにmy を割り当てるときに、 を呼び出しfindViewById(android.R.id.tabhost)ました。これは、何らかの理由で、私の問題を解決しました。

于 2013-01-26T19:36:30.267 に答える