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:na开发者_如何学JAVAme="TrainsActivity">
</activity>
And I'm trying to get TrainsListView:
mListView = (ListView) findViewById(R.id.TrainsListView);
But after this mlistView is null. Why?
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.
If findViewById() is failing to find your target id, here are some reasons this may happen:
[[ In your case, I suspect #1 or #2.]
SetContentView()(orinflate) hasn't been called yet to associate a layout to search yet.SetContentView()(orinflate) was called with a layout that does not contain the target id, e.g. you specified the wrong layout.There's a typo in the
idin the layout or in the code.You've imported the wrong
Rfile (like a library component'sRfile) and coincidentally the same id was also used in the otherRfile).You're searching the wrong view, e.g. you inflated a menu layout and did not attach it to the root layout and then you attempted to search for an id in the root layout (
findBiewById(..)) when you meant to search the inflated menu layout (menu.findBiewById(..)).
加载中,请稍侯......
精彩评论