-1

oncreateでこのコードを使用して垂直タブを作成しようとしています。タブを追加し、tabHandlerでボタンonclickを使用して、表示するタブを設定します。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     tabHost = (TabHost)findViewById(android.R.id.tabhost);



     TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
     TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
    firstTabSpec.setIndicator("First Tab Name").setContent(new         Intent(this,PageOne.class));
     secondTabSpec.setIndicator("Second Tab Name").setContent(new Intent(this,PageTwo.class));
    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
}
public void tabHandler(View target){
    //artistButton.setSelected(false);
   // albumButton.setSelected(false);
    //songButton.setSelected(false);
    if(target.getId() == R.id.artist_id){
        tabHost.setCurrentTab(0);

       // artistButton.setSelected(true);
    } else if(target.getId() == R.id.album_id){
       tabHost.setCurrentTab(1);
       // albumButton.setSelected(true);
    } else if(target.getId() == R.id.song_id){
      tabHost.setCurrentTab(2);
       // songButton.setSelected(true);
    }
}

これはmain.xmlです

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal" 
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <FrameLayout android:layout_width="0dip" 
        android:layout_height="fill_parent" android:layout_weight="0.2">
    <TabWidget android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:visibility="gone"/>
        <LinearLayout android:layout_width="fill_parent"  
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <Button android:layout_height="0dip" 
                android:layout_width="fill_parent" 
                android:layout_weight="1.0"

                android:id="@+id/artist_id" 
                android:onClick="tabHandler"/>
            <Button android:layout_height="0dip" 
                android:layout_width="fill_parent" 
                android:layout_weight="1.0"

                android:id="@+id/album_id" 
                android:onClick="tabHandler"/>
            <Button android:layout_height="0dip" 
                android:layout_width="fill_parent" 
                android:layout_weight="1.0"

                android:id="@+id/song_id" 
                android:onClick="tabHandler"/>
    </LinearLayout> 
</FrameLayout>       
<FrameLayout android:id="@android:id/tabcontent" 
    android:layout_width="0dip" 
    android:layout_height="fill_parent" android:layout_weight="0.8"/>

ただし、実行時にこの例外がスローされます

01-21 19:01:03.655: E/AndroidRuntime(1101): FATAL EXCEPTION: main
01-21 19:01:03.655: E/AndroidRuntime(1101): java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.exampleexample.   .MainActivity}: java.lang.NullPointerException

これらの2行が追加されたとき:tabHost.addTab(firstTabSpec); tabHost.addTab(secondTabSpec);

でも理由はわかりません

4

2 に答える 2

1

ここにコードを入力してくださいリソースにアクセスするためにRだけでなくandroid.Rを使用しているようです。android.Rアプリに固有ではないAndroidリソースにアクセスしますTabHost。作成したxmlファイルの内部にアクセスする必要があると想定しています。

tabHost = (TabHost)findViewById(android.R.id.tabhost); 

次のように変更する必要があります:

tabHost = (TabHost)findViewById(R.id.tabhost); 
于 2013-01-21T16:58:42.050 に答える
1

問題は、TabActivityを拡張する代わりにアクティビティを拡張することでした

于 2013-01-27T19:41:04.877 に答える