为什么我的Android应用程序不断告诉我,当它已经被定义时,我需要定义一个ListView ID?
每当我尝试运行我的应用程序,以下异常被抛出:为什么我的Android应用程序不断告诉我,当它已经被定义时,我需要定义一个ListView ID?
01-22 00:40:51.868:ERROR/AndroidRuntime(2219):了java.lang.RuntimeException:无法启动活动ComponentInfo {[节录]}:了java.lang.RuntimeException:你的内容必须有一个ListView其id属性为 'android.R.id.list'
这里是有问题的ListView中的标记:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"></ListView>
所以,因为id被明确定义为“list”,这里给出了什么?在过去的几个小时里,我已经多次运行我的代码,试图追踪这个原因,我很难过。任何人都可以提供任何指针?
嗨托德 无论你给XML中的列表视图的ID 这个ID你想在Java类中声明。在XML
示例代码 &在java类
的ListView列表=(ListView中)findViewById(R.id.list);
我想你有意外导入android.R包。
删除导入,你应该没问题。
可能是你ComponentInfo
类的扩展因此ListActivity
确保您ListView
对象必须具有以下属性在布局XML文件:
android:id="@android:id/list"
其实有android.R.id.list之间的”差异“和简单的 ”R.id.list“
<ListView android:id="@android:id/list" />
是不一样的列表,
<ListView android:id="@+id/list" />
第一个在android框架中有一个预定义的id,这意味着有些东西是在后台为你自动创建的。考虑下面的例子:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="match_parent"
android:height="match_parent">
<ListView
android:width="match_parent"
android:height="match_parent"
android:id="@android:id/list" />
<TextView
android:width="wrap_content"
android:height="wrap_content"
android:id="@android:id/empty"
android:text="You don\'t have any items yet." />
</LinearLayout>
鉴于上述布局在XML和你对应的Java类扩展ListActivity,TextView的会自动显示了您,当您的列表没有任何项目,它也将在列表获得第一个项目时自动隐藏。
如果你测试你是真正的XML,你会发现它实际上是@namespace:name/ressource - >“@android:id/list”。你应该编辑你的答案并修复所有的@ id/android:。这是错误的 – 2011-01-22 10:13:05
你能不能请求xml的粘贴代码,你定义了listview和jave文件的代码,这样你就可以得到这个id了。 – 2011-01-22 08:59:43