2012-03-29 11 views
0

このTabHostを動作させようとしています。私がXMLレイアウトにTabHostを追加してプログラムを実行すると、各タブの異なるコンテンツが互いに重ねて表示されます。レイアウトが適切に動作していることがわかります。次に、スペックコードをアクティビティに追加し、この画面に移動するとエミュレータ上のプログラムを終了します。カップルの可能性が頭に浮かぶTabHost androidが正しく動作しない

  welcomeName = (TextView) findViewById(R.id.tWelcome); 
    Test = (TextView) findViewById(R.id.tTest); 
    newTask = (Button) findViewById(R.id.bNewTask); 
    myTask = (ListView) findViewById(R.id.listView); 
    TabHost th = (TabHost) findViewById(R.id.myTabs); 

    welcomeName.setText("Welcome " + ToDoActivity.myUser.getName() + "!"); 

    th.setup(); 
    TabSpec specs = th.newTabSpec("tag1"); 
    specs.setContent(R.id.tab1); 
    specs.setIndicator("All"); 
    th.addTab(specs); 
    specs = th.newTabSpec("tag2"); 
    specs.setContent(R.id.tab2); 
    specs.setIndicator("Personal"); 
    th.addTab(specs); 
+0

あなたが代わりに '' TabHost'のTabLayout'を使用しない理由は? –

答えて

0

(重要部分のみ...):ここでは

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/tWelcome" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/bNewTask" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Add Task" /> 

<TabHost 
    android:id="@+id/myTabs" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

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

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

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

       <ListView 
        android:id="@+id/listView" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" > 
       </ListView> 
      </LinearLayout> 

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

       <TextView 
        android:id="@+id/tTest" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Test" 
        android:textAppearance="?android:attr/textAppearanceLarge" /> 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

</LinearLayout> 

が今の私の活動コードです:
だからここに私のXMLです。

TabActivityを使用している場合(要件ではないが、作業が楽になる場合)、タブホストのIDはandroid:id="@android:id/tabhost"でなければなりません。

私はタブーホストがルートノードでなければならないとも言いました。私はアンドロイドのドキュメントを使用して確認できません。私はおそらく、他のすべての道が失敗した(または誰かがそれが真実であることを確認した)後、この変更を行うだろう。

TabActivityを使用している場合は、idでタブホストを参照する必要はなく、setup()メソッドを使用することもできます。私はあなたがsetup()メソッドを使用しているのを見ていますが、あなたの活動がTabActivityであるかどうかはわかりません。もしそうなら、あなたの問題を引き起こしているかもしれません。

以下は、私がタブレイアウトの設定に使用する方法です。それは私のために働きます。これはレイアウトのルートノードとしてタブホストを想定しており、それはidとしてandroid:id/tabhostを持ち、TabActivityです。

tabhostコード:

public class Tabs extends TabActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.your_layout); 

     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 

     spec = tabHost.newTabSpec("tag1").setIndicator("All").setContent(R.id.tab1); 
     tabHost.addTab(spec); 
     spec = tabHost.newTabSpec("tag2").setIndicator("Personal").setContent(R.id.tab2); 
     tabHost.addTab(spec); 
    } 
} 

・ホープ、このことができます! (問題を解決する場合は、必ず答えを受け入れるためにチェックマークをクリックしてください)。

+0

getTabHost()は私が定義したメソッドではありません。私はこのメソッドをTabHost Idまたは何かを返すようにしますか? – user433047

+0

TabActivityを拡張している場合は、そこからメソッドです。 – Barak

+0

TabActivityは推奨されていません。私はActionBarSherlock http://actionbarsherlock.com/を使用し、新しいActionBar APIのみを使用してタブを作成します。これにより、Ice Cream Sandwichへの移行がさらに楽になり、次の半年に新しいデバイスがたくさん登場します。 – Janusz

0

これは、タブのためのAndroidのチュートリアルページです: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

それは、タブの情報と要件を持っており、それは、サンプルコードの多くを提供します。そこからコピー貼り付けして編集することができます。

具体的には、私が提案アンドロイドのIDであることを、すべてのタブの要素のIDを変更したい:

android:id/tabhost 
android:id/tabs 
android:id/tabcontent 
関連する問題