Android ListView findViewById返回null

Android ListView findViewById返回null,android,listview,android-listview,Android,Listview,Android Listview,我没有使用ListActivity,因为我想扩展FragmentActivity。相反,我尝试使用: ListView lv = (ListView) findViewById(R.id.mainListView); 不幸的是,lv是空的 在我的xml中,我有: <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainListView" a

我没有使用ListActivity,因为我想扩展FragmentActivity。相反,我尝试使用:

ListView lv = (ListView) findViewById(R.id.mainListView);
不幸的是,lv是空的

在我的xml中,我有:

 <ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

我也尝试过在列表视图中使用不同的android id,比如在使用ListActivity时必须使用的id,但它总是空的

编辑:在调用setContentView之前,我试图调用findViewById。首先调用setContentView将其修复。

尝试:

ListView lv = (ListView) view.findViewById(R.id.mainListView);
这可能对你有帮助

 xmlns:android="http://schemas.android.com/apk/res/android"
将其从xml的Listview中删除,并使用

<ListView
    android:id="@+id/mainListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>


正如JRaymond不久前在一篇评论中提到的,我需要在使用findViewById之前调用setContentView。

您应该将
setContentView
放在
findViewById
的顶部,您可以显示为您的整个布局吗?您在调用
setContentView()之前调用它吗
?请提供完整的活动code@Andy如果在
setContentView()之后调用,请尝试清理/构建并重新启动eclipse。您好,JRaymond,我在调用setContentView之前尝试使用findViewById。如果你想补充一个答案,我会接受的。下次我将提供更多的代码。