Android ListActivity致命异常

时间:2013-01-31 02:21:35

标签: java android listview exception listactivity

每当我尝试开始此活动时,我都会收到此错误:

01-31 02:19:19.558: E/AndroidRuntime(1318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prva/com.example.prva.ListView}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

ListActivity:

package com.example.prva;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;


public class ListView extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        fillData();
        }

    private void fillData() {
        // Get all of the notes from the database and create the item list
        Cursor c = DatabaseManager.getAllData();
        startManagingCursor(c);

        String[] from = new String[] { DatabaseManager.TABLE_COLUMN_ONE };
        int[] to = new int[] { R.id.text1 };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
        setListAdapter(notes);
    }
    }

我的listview xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="326dp" >
    </ListView>

</LinearLayout>

我不明白这是因为我有一个listview,其中id是list?

2 个答案:

答案 0 :(得分:1)

您必须使用android.R.id.list下的默认@android:id@+id创建名为list自己的 ID。

所以,而不是:

android:id="@+id/list"

使用以下内容:

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

答案 1 :(得分:1)

将您的ListView ID更改为@android:id/list这将解决您的问题..列表活动应该包含@android:id/list的列表视图..我希望这会对您有所帮助