2013-05-03 41 views
0

我在顯示列表視圖時顯示錯誤。 錯誤說:顯示列表視圖時的非法狀態異常

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) 

列表視圖代碼如下:

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和我dnt noe從這是顯示這個! 請幫忙!謝謝!

回答

2

從Android開發者網站

適配器從一個遊標到ListView控件公開數據。光標必須包含一個名爲「_id」的列,否則這個class將不起作用。

您正在嘗試使用SimpleCursorAdapter的遊標,它需要一個名爲_id的列。就像編輯表格創建語句並添加一個名爲_id的列一樣簡單。聲明應如下

_id INTEGER PRIMARY KEY AUTOINCREMENT

+1

嘿非常感謝!這真的很有幫助!最後問題解決了! – shivani 2013-05-03 10:03:12

+0

謝謝。隨時歡迎 – 2013-05-03 10:13:53