textView的findViewById返回null

时间:2011-09-04 11:40:01

标签: android

基本上我的问题很常见。但是我有谷歌和研究所有可能的解决方案,但没有用,因此要求SO解决它。

我的问题是为什么当我为textview找到ViewById时,我在返回时得到一个null。下面是我的活动和XML文件。

的活动:

public class Chicken extends ListActivity {

private TextView dishName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] chicken = getResources().getStringArray(R.array.chicken_array);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
            chicken));

    dishName = (TextView) findViewById(R.id.TextView02);
    Log.w("test", "ok " + dishName); // returns null

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Toast.makeText(getApplicationContext(),
                    ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });
}
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@+id/LinearLayout1"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="5dp">
    <TabWidget android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <FrameLayout android:layout_alignParentLeft="true"
        android:layout_below="@android:id/tabs" android:id="@android:id/tabcontent"
        android:layout_width="185dp" android:layout_height="fill_parent"
        android:padding="5dp">
    </FrameLayout>
    <RelativeLayout android:layout_toRightOf="@android:id/tabcontent"
        android:id="@+id/relativeLayout1" android:layout_height="fill_parent"
        android:layout_alignTop="@android:id/tabcontent"
        android:layout_gravity="right" android:layout_width="115dp"
        android:background="@drawable/brown" android:gravity="fill_vertical"
        android:layout_alignRight="@android:id/tabs">
        <TextView android:textColor="#000000" android:layout_height="wrap_content"
            android:text="@string/order" android:paddingTop="3dp"
            android:layout_width="wrap_content" android:id="@+id/textView1"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"></TextView>
        <Button android:layout_height="wrap_content"
            android:layout_width="105dp" android:text="Send" android:id="@+id/button1"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"></Button>
        <FrameLayout android:id="@+id/frameLayout1"
            android:padding="3dp" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_below="@+id/textView1"
            android:layout_alignLeft="@+id/button1" android:layout_above="@+id/button1"
            android:layout_alignParentRight="true">
            <ScrollView android:id="@+id/ScrollView01"
                android:layout_width="wrap_content" android:layout_height="wrap_content">
                <TableLayout android:id="@+id/tableLayout01"
                    android:shrinkColumns="1" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" android:layout_below="@+id/textView1"
                    android:layout_alignLeft="@+id/button1">
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow01">
                        <TextView android:id="@+id/TextView01"
                            android:textColor="#000000" android:layout_width="25dp"
                            android:layout_height="wrap_content" android:text="x1"></TextView>
                        <TextView android:id="@+id/TextView02" android:text="chicken"
                            android:textColor="#000000" android:layout_width="wrap_content"
                            android:layout_height="wrap_content"></TextView>
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow02">
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow03">
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow04">
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow05">
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow06">
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow07">
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow08">
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow09">
                    </TableRow>
                    <TableRow android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/tableRow010">
                    </TableRow>
                </TableLayout>
            </ScrollView>
        </FrameLayout>
    </RelativeLayout>
</RelativeLayout>
</TabHost>

希望有人可以帮我解决这个问题。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

您必须使用setContentView告诉活动要膨胀和设置的布局。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout); // Assuming your layout is called mylayout.xml

    String[] chicken = getResources().getStringArray(R.array.chicken_array);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
            chicken));

    dishName = (TextView) findViewById(R.id.TextView02);
    Log.w("test", "ok " + dishName); // returns null

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

答案 1 :(得分:0)

您可能需要创建textview的gloabl引用,因为它是您为tab活动指定的xml布局中的。但是您尝试从其子活动(用于表示选项卡的活动)访问textview。在子活动中使用findViewById时,它将查看为活动指定的布局或视图。

编辑:使用应用上下文的方式。

扩展应用程序类并添加textview类型的属性。因此,在您的活动中,您可以访问应用程序上下文并将文本视图设置为属性。

MyApplication appContext = (MyApplication) getApplicationContext();
appContext.textview = (TextView) findViewById(R.id.textview1);

在任何其他活动中,您可以以相同的方式访问该文本视图。

MyApplication appContext = (MyApplication) getApplicationContext();

This will have the textview appContext.textview
相关问题