Android:id列表视图
我在xml中声明了一个列表视图:Android:id列表视图,android,listview,Android,Listview,我在xml中声明了一个列表视图: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertic
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#F7EFE8">
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_message"/>
</LinearLayout>
但是当我试图向java声明/使用它时,找不到id。但是,当我将列表视图重命名为其他名称并尝试使用它时,我可以看到它“R.id.”,但当R.id.list出现时,我找不到它。如果我没有使用android:list,就会出现一个错误,即应该有一个id为android:list的列表视图。有人帮忙吗 在xml布局使用中
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
如果您需要将android id用于listview,请将代码替换为
<ListView
android:id="@+android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
它可能会帮助您..试试这个:android:id=“@+id/list”
然后清理项目并重建。
在java中,只需如下调用findViewById:
ListView lv = (ListView)findViewById(R.id.list);
Ref:您的ListView对象id应指定为android:id=“@android:id/list”
ListView lv = (ListView) findViewById(android.R.id.list);
或者它应该有一些其他id,比如android:id=“@+id/sampleList”
ListView lv = (ListView) findViewById(R.id.sampleList);
选中此项:
及
希望这会有所帮助。您的布局中是否还有其他内容,或者您没有提到的其他细节?我尝试了你在一个干净的(带有xml头的)布局文件中提供的代码片段,但它不起作用。你使用了什么代码?你使用了我答案的第一部分吗?请发布您的错误。无需在第二部分的android id前面添加
+
,因为此id已定义。这可能就是为什么它对OP不起作用的原因
ListView lv = (ListView) findViewById(R.id.sampleList);