2012-04-24 43 views
0

空我有NewTransferActivity類,它擴展TabActivity。代碼:findViewById()返回在TabActivity類

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> 

我想加載佈局從XML文件中的單個標籤,我definded。不幸的是,我得到NullPointerException,因爲方法getTransferTab()中佈局爲null。 'LinearLayout layout =(LinearLayout)findViewById(R.id.mytrans)'返回null。我做錯了什麼?提前致謝。

回答

1

你還是嘗試使用LayoutInflater從XML獲取視圖層次,

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

完美的答案,但insread R.id.mytrans我不得不使用R.layout.layout_name。十分感謝。 – problemgenerator 2012-04-24 12:50:19

1

我的第一個想法是,在這條線:

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

您所呼叫的你TabContentFactory

findViewById方法你需要調用位於Activity該命令的版本。

+0

嗨。我已將此行更改爲LinearLayout layout =(LinearLayout)NewTransferActivity.this.findViewById(R.id.mytrans);但結果仍然爲空。我也試圖在我的Activity類中寫一個方法,它只是返回一個LinearLayout:private LinearLayout idRes(){LinearLayout layout =(LinearLayout)findViewById(R.id.mytrans);返回佈局; }並在getReceiverTab()中調用它,但它不正確 – problemgenerator 2012-04-24 12:40:40

+0

在SetContentView之後直接調用findViewById以確保您獲得正確的視圖。也許你正在加載錯誤的佈局。 – Kuffs 2012-04-24 12:48:31