ListView findViewById返回null [英] ListView findViewById returning null
问题描述
我不使用ListActivity,因为我想扩展FragmentActivity.相反,我尝试使用:
I'm not using a ListActivity because I want to extend FragmentActivity. Instead I'm trying to use:
ListView lv = (ListView) findViewById(R.id.mainListView);
不幸的是lv为空.
在我的xml中,我有:
In my xml I have:
<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,但始终为Null.
I've also tried using different android id's for the list view such as the one you have to use when you use ListActivity, but it's always Null.
我试图在调用setContentView之前调用findViewById.首先调用setContentView对其进行修复.
I was trying to call findViewById before calling setContentView. Calling setContentView first fixed it.
推荐答案
正如JRaymond不久前在评论中提到的那样,在使用findViewById之前,我需要先调用setContentView.
As JRaymond mentioned in a comment awhile ago, I needed to call setContentView before using findViewById.
这篇关于ListView findViewById返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!