我没有使用 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/