我有 NewTransferActivity 类,它扩展了 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 文件中加载单个选项卡的布局。不幸的是,我收到 NullPointerException,因为布局在方法 getTransferTab() 中为空。'LinearLayout 布局 = (LinearLayout) findViewById(R.id.mytrans)' 返回 null。我做错了什么?提前致谢。