2012-01-08 3 views
0

私はAndroidにタブホストを実装しようとしています。私はそれを設定するという点で正しくeveythingをやったが、私はページを実行すると、私は私のAndroid携帯電話上のtabhostコントロールを表示し、次のスタックトレースを生成しませんnullポインタ例外を取得:Androidタブーが動作しない

Radio_Debug [Android Application] 
     DalvikVM[localhost:8604]  
      Thread [<3> main] (Suspended (exception NullPointerException)) 
       TabHost.dispatchWindowFocusChanged(boolean) line: 295 
       LinearLayout(ViewGroup).dispatchWindowFocusChanged(boolean) line: 664 
       FrameLayout(ViewGroup).dispatchWindowFocusChanged(boolean) line: 664  
       LinearLayout(ViewGroup).dispatchWindowFocusChanged(boolean) line: 664 
       PhoneWindow$DecorView(ViewGroup).dispatchWindowFocusChanged(boolean) line: 664 
       ViewRoot.handleMessage(Message) line: 1819 
       ViewRoot(Handler).dispatchMessage(Message) line: 99 
       Looper.loop() line: 123 
       ActivityThread.main(String[]) line: 4363  
       Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
       Method.invoke(Object, Object...) line: 521 
       ZygoteInit$MethodAndArgsCaller.run() line: 860 
       ZygoteInit.main(String[]) line: 618 
       NativeStart.main(String[]) line: not available [native method] 
      Thread [<17> Binder Thread #4] (Running)  
      Thread [<15> Binder Thread #3] (Running)  
      Thread [<13> Binder Thread #2] (Running)  
      Thread [<11> Binder Thread #1] (Running)  

Javaコードは、XMLページのコードにリンクされている次のとおりです。

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TabHost; 


public class tabs extends Activity{ 


    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabmain); 

     TabHost tabHost = (TabHost)findViewById(R.id.tabHost); 
     tabHost.setup(); 


    } 



} 

次のようにtabhostコントロールを収容するxmlファイルのレイアウトは次のとおりです。

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

      <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> 
       <TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget> 

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

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


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


        </LinearLayout> 
        <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab3"> 


        </LinearLayout> 
        <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab4"> 


        </LinearLayout> 

       </FrameLayout> 
      </LinearLayout> 
     </TabHost> 




    </LinearLayout> 
+0

このタブホストの内部にはビューがありますか? – kosa

+0

はい、4つのテキストビューで試しましたが、私はまだ同じエラーが発生しています。 – user615099

答えて

4

あなたは決してestabliいずれかのタブを押します。 のnewTabSpec()addTab()メソッドに電話して、タブを設定し、TabWidgetの子を指す必要があります。これを実証するHere is a sample application

+0

私はあなたがそのポイントを見逃していると思います。これまでのコードでも、エラーが発生してはいけません。私はそれ以来、タブとテキストビューで試してみましたが、私はまだ同じエラーが発生しています。私は何が欠けているかも知っていますか? – user615099

+1

@ user615099: "これまでのコードでも、エラーは発生しません" - 空の 'TabHost'があるとエラーになります。 "私が何が欠けているかも知っていますか?"私の頭の上から外して、いいえ、私はそれがうまくいくサンプルを指していたので、あなたのコードとサンプルの間で何が違うのかを調べることができます。 – CommonsWare

+0

@CommonsWareあなたは男です:)あなたの小さなサンプルは、私のタブホストでレイアウトが間違っていたことを理解するのを助けました.....多くの感謝:) –

関連する問題