开发者

Why does my Android app keep telling me I need to define a ListView id when it is already defined?

Whenever I try to run my app, the following exception is thrown:

01-22 00:40:51.868: ERROR/AndroidRuntime(2219): java.lang.RuntimeException: Unable to start activity ComponentInfo{[Redacted]}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Here is the markup for the ListView in question:

<ListView 
 android:id="@+id/list" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 android:dividerHeight="1px"></ListView>

So, since the id is clearly defined as "list," what gives here? I've run through my code multiple times in the past couple of hours trying to 开发者_C百科track down the cause of this and I'm stumped. Can anyone offer any pointers?


Probably your class ComponentInfo is extending the ListActivity therefore insure that your ListView object must have the following attribute in your layout XML file:

android:id="@android:id/list"


Actually there is a difference between "android.R.id.list" and simply "R.id.list"

<ListView android:id="@android:id/list" />

is not the same list as

<ListView android:id="@+id/list" />

The first one has a predefined id in the android framework which means that some things are made automatically in the background for you. Consider the example below:

<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>

Given the above layout in your XML and that your corresponding java class extends the ListActivity, the TextView will automatically be shown for you when your list doesn't have any items and it will also automatically be hidden when the list get its first item.


I guess you have accidentl imported android.R package.

remove the import and you should do fine.


hi Todd whatever u give listview's id in XML thst id u want to declare in java class.

sample code in XML & In java class

ListView list=(ListView)findViewById(R.id.list);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜