Alex
Alex

Reputation: 3

findViewById for textview returns null

Basically my question is quite common. However I have google and research all the possible solutions but to no avail thus asking on SO to resolve it.

My question is why when I findViewById for a textview, i got a null when returning back. Below is my activity and the XML file.

Activity:

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>

Hope somebody can help me out with this one. Thanks in advance.

Upvotes: 0

Views: 4633

Answers (2)

blessanm86
blessanm86

Reputation: 31779

You may need to create a gloabl reference to the textview as it is in the xml layout you specify for the tab activity. But you are trying to access the textview from its child activity(activity used to represent a tab.). When you use findViewById in the child activity it will look into the layout or view that was specified for the activity.

EDIT: Way to use app context.

Extend the application class and add a attribute of type textview. So in your activity you can access the application context and set the text view a attribute.

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

In any other activity you can access that text view the same way.

MyApplication appContext = (MyApplication) getApplicationContext();

This will have the textview appContext.textview

Upvotes: 0

ccheneson
ccheneson

Reputation: 49410

You have to use setContentView to tell the activity which layout to inflate and be set to.

@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);

Upvotes: 2

Related Questions