0

我有 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。我做错了什么?提前致谢。

4

2 回答 2

1

我的第一个想法是,在这一行中:

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

你正在调用你的findViewById方法TabContentFactory

您需要调用位于Activity.

于 2012-04-24T12:24:08.877 回答
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;
        }
    };
于 2012-04-24T12:34:57.243 回答