Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not get element from layout

I have 2 activities. The 2nd .xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView 
            android:id="@+id/TrainsListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
        />
</LinearLayout>

Part of manifest:

<activity android:name="TrainsActivity">

</activity>

And I'm trying to get TrainsListView:

mListView = (ListView) findViewById(R.id.TrainsListView);

But after this mlistView is null. Why?

like image 556
artem Avatar asked Feb 04 '11 12:02

artem


1 Answers

I'm guessing this is happening in your Activity's onCreate() method because you are calling findViewById() before you have called setContentView(). If that's not the case then please show more of your code.

like image 82
Mark B Avatar answered Sep 28 '22 15:09

Mark B