显示列表视图中的illegalstateexception

时间:2013-05-03 08:16:53

标签: android listview illegalstateexception

我在listview中显示我的内容时遇到错误。 错误说:

 05-03 08:00:13.575: E/AndroidRuntime(3341): java.lang.RuntimeException: Unable to start activity                    ComponentInfo{com.android.lthomepage/com.android.lthomepage.ListViewDetails}: java.lang.IllegalArgumentException:    column '_id' does not exist
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.os.Looper.loop(Looper.java:137)
05-03 08:00:13.575: E/AndroidRuntime(3341): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:93)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at com.android.lthomepage.ListViewDetails.displaylistView(ListViewDetails.java:77)
05-03 08:00:13.575: E/AndroidRuntime(3341):     at       com.android.lthomepage.ListViewDetails.onCreate(ListViewDetails.java:42)

listview代码如下:

    Cursor cursor=way.fetchDetails();
    String[] columns=new String[] {WayDataBase.KEY_NAME,WayDataBase.KEY_APPNO,WayDataBase.KEY_AMOUNT};

    int[] to=new int[] {R.id.name,
            R.id.continent,
            R.id.region};

    adapter=new SimpleCursorAdapter(this, R.layout.country_info, cursor, columns, to, 0);

    ListView listview=getListView();
    listview.setAdapter(adapter);

数据库代码是:

public Cursor fetchDetails()
{

    createAllValuesTable();
    Cursor mCursor=db.query(AllValuesTable, new String[] {KEY_NAME,KEY_APPNO,KEY_AMOUNT}, null, null, null, null, null);

     if (mCursor != null) {
           mCursor.moveToFirst();
          }
          return mCursor;
}

我似乎无法弄清楚的问题是什么? 我没有任何专栏作为_id,我不知道是不是它显示了这个! 请帮忙!谢谢!

1 个答案:

答案 0 :(得分:2)

来自Android开发者网站

将数据从Cursor公开到ListView小部件的适配器。游标必须包含名为“_id”的列,否则此class将无效。

您正在尝试使用SimpleCursorAdapter的游标,该游标需要名为_id的列。它就像编辑表创建语句和添加名为_id的列一样简单。声明应如下

_id INTEGER PRIMARY KEY AUTOINCREMENT