findviewbyid 空指针的问题

各位大侠,最近发现一个问题个人觉得非常诡异。

在oncreate 中 listView_test = (ListView) this.findViewById(R.id.listcardinfo); 为null。
但是 同一个页面中的一个button的 onClick方法确可以找到这个listview,并且赋值。

附上XML

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="测试"
            android:textColor="#000000"
            android:textSize="22.0dip" />

    <include layout="@layout/home_title_layout"/>
    </RelativeLayout>




    <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

</LinearLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listcardinfo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

确认findViewById使用的时机,你要在setcontentview之后再用,如果你用在了setcontentview之前,肯定找不到

有些奇怪,我并未在xml里面找到你所说的按钮,也有些好奇你是不是将listview的那个layout,include到其他页面中
假如是include到其他页面的话,再从其他页面使用(ListView) this.findViewById(R.id.listcardinfo)来寻找listview的id是不行的,应该是先由this.findViewById(R.id.所include的layout的id)找到那个view,然后再由(ListView) view.findViewById(R.id.listcardinfo)来找到listview,这样才不会报null

你的list 在 LinearLayout 里面
你应该先获取 到 LinearLayout 的 对象 然后 用 LinearLayout. findViewById();
直接调用findViewById()使用的是setcontentview()获得的对象的findViewById方法。

或者你是否后面有改变视图来源,开始没有,后面的视图有了

http://bbs.csdn.net/topics/390354932