findViewById()在TabActivity类中返回null

时间:2012-04-24 12:16:43

标签: android

我有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()方法中layout是null。 'LinearLayout layout =(LinearLayout)findViewById(R.id.mytrans)'返回null。我做错了什么?提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

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

您正在调用findViewById

TabContentFactory方法

您需要调用位于Activity

中的该命令的版本

答案 1 :(得分: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;
        }
    };
相关问题