2012-04-24 4 views
0

私は、TabActivityを継承するNewTransferActivityクラスを持っています。コード:レイアウトを含むfindViewById()は、TabActivityクラスのnullを返します。

public class NewTransferActivity extends TabActivity { 

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

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

    spec = tabHost.newTabSpec("receiver").setIndicator("Rec").setContent(getReceiverTab()); 
    tabHost.addTab(spec); 

    spec = tabHost.newTabSpec("transfer").setIndicator("Trans").setContent(getTransferTab()); 
    tabHost.addTab(spec); 

} 

private TabContentFactory getReceiverTab(){ 
    TabContentFactory factory = new TabContentFactory() { 
     @Override 
     public View createTabContent(String tag) { 

      ... 

      return layout; 
     } 
    }; 
    return factory; 
} 

private TabContentFactory getTransferTab(){ 
    TabContentFactory factory = new TabContentFactory() { 

     @Override 
     public View createTabContent(String tag) { 
      LinearLayout layout = (LinearLayout) findViewById(R.id.mytrans); 

      if (layout == null){ 
       //layout = new LinearLayout(NewTransferActivity.this); 
       System.out.println("NULL>>>>>>"); 
       return new LinearLayout(NewTransferActivity.this); 
      } 
      return layout; 
     } 
    }; 
    return factory; 
} ... 

私のxmlファイルには、次のとおりです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mytrans" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <requestFocus /> 
</EditText> 


<TextView 
    android:id="@+id/textView2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 


<TextView 
    android:id="@+id/textView3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 


<TextView 
    android:id="@+id/textView4" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText4" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

私はdefinded xmlファイルから単一のタブのレイアウトをロードします。 UnfortunatellyメソッドはgetTransferTab()のレイアウトがnullであるため、NullPointerExceptionが発生します。 'LinearLayout layout =(LinearLayout)findViewById(R.id.mytrans)'はnullを返します。私が間違っていることは何ですか?前もって感謝します。

LinearLayout layout = (LinearLayout) findViewById(R.id.mytrans); 

あなたはあなたがActivityに位置しているコマンドのバージョンを呼び出す必要があり、あなたのTabContentFactory

findViewByIdメソッドを呼び出している:

答えて

1

TabContentFactory factory = new TabContentFactory() { 
     @Override 
     public View createTabContent(String tag) { 

      LayoutInflater inflater = LayoutInflater.from(Activity_Name.this); 
      View linearLayout = inflater.inflate(R.layout.mytrans, null); 

      return linearLayout; 
     } 
    }; 
+0

完璧な答えですが、私はR.layout.layout_nameを使用しなければなりませんでした。本当にありがとう。 – problemgenerator

1

私の最初の考えは、この行では、ということです。あなたはむしろ、XMLからビュー階層を取得するためにLayoutInflaterを使用してみてください

+0

こんにちは。この線をLinearLayout layout =(LinearLayout)NewTransferActivity.this.findViewById(R.id.mytrans)に変更しました。結果はまだnullです。私は、単に、LinearLayoutを返すアクティビティクラスにメソッドを書き込もうとしました:private LinearLayout idRes(){LinearLayout layout =(LinearLayout)findViewById(R.id.mytrans);レイアウトを返す。 } getReceiverTab()で呼び出しましたが、正しくありません – problemgenerator

+0

正しいビューを取得していることを確認するために、SetContentViewの直後にfindViewByIdを呼び出します。たぶん間違ったレイアウトをロードしています。 – Kuffs

関連する問題