Unable to get item from layout.

I have 2 activities. The second .xml looks like this:

<?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 the manifest:

                                      

<activity android:name="TrainsActivity">

</activity>

And I'm trying to get a TrainsListView:

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

But after that, mlistView is null. Why?

+5
source share
2 answers

I assume this happens in your Activity method onCreate()because you are calling findViewById()before you are calling setContentView(). If not, please show more of your code.

+12
source

If findViewById()your target identifier cannot be found, here are some reasons why this could happen:

[[In your case, I suspect # 1 or # 2.]

  • SetContentView() ( inflate) , .

  • SetContentView() ( inflate) , , . .

  • id .

  • R (, R), R).

  • , . , (findBiewById(..)), (menu.findBiewById(..)).

+6

All Articles