android - ListView findViewById 返回 null

标签 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"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

我也尝试过为 ListView 使用不同的 android id,例如在使用 ListActivity 时必须使用的 android id,但它始终为 Null。

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

最佳答案

正如 JRaymond 刚才在评论中提到的,我需要在使用 findViewById 之前调用 setContentView。

关于android - ListView findViewById 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12468473/

相关文章:

android - 前置摄像头 - 保存前正确镜像和旋转

android - 我如何从 styles.xml 设置 RelativeLayout 的样式 - Android

android - 在自定义 ListView 中一次只选择一个单选按钮,android

android - Android 中的记事本 - 崩溃 SimpleCursorAdapter

Android ListView 项目选择问题

android - 如何在不使用 eclipse 的情况下调试/单步执行 android 应用程序

java - GCM 推送通知实现

listview - 如何找到下一个未选中的 ListView 项?

qt - Qml ListView 空行占位符就像 TableView 中一样

android - 如何检查listitem是否在listview android中被选中?