2013-05-03 59 views
0

我要實現它是通過SQL遊標我已經成功地實現了人口光標一個ListView和我有麻煩適應它 這是我的數據庫處理器在其中創建光標的ListView通過光標實現.Error

public Cursor fetchAllSuspects() { 
     SQLiteDatabase db = this.getReadableDatabase(); 
      Cursor mCursor = db.query(TABLE_SUSPECTS, new String[] {KEY_ID, 
        KEY_NAME, KEY_SEX}, 
      null, null, null, null, null); 

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

則接着呼籲inmy suspectview類和實現如下

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_suspectview); 

    DatabaseHandler db = new DatabaseHandler(this); 
    Cursor lol = db.fetchAllSuspects(); 
list = (ListView) findViewById(android.R.id.list); 

String[] columns = new String[] { 
    // DatabaseHandler.KEY_ID, 
     DatabaseHandler.KEY_NAME, 
     DatabaseHandler.KEY_SEX   
     }; 
int[] to = new int[] { 
     // R.id.suspect_id, 
      R.id.suspect_name, 
      R.id.suspect_sex 
      }; 

dataAdapter = new SimpleCursorAdapter(
      this, 
      R.layout.suspect_list, 
      lol, 
      columns, 
      to, 
      0); 

list.setAdapter(dataAdapter); 

我收到並在logcat的至極狀態錯誤字段ID爲不存在,但存在並保持6個條目數據的logcat的是低於

05-03 19:08:19.951: D/dalvikvm(582): Not late-enabling CheckJNI (already on) 
05-03 19:08:20.951: I/dalvikvm(582): threadid=3: reacting to signal 3 
05-03 19:08:21.101: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt' 
05-03 19:08:21.341: I/dalvikvm(582): threadid=3: reacting to signal 3 
05-03 19:08:21.441: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt' 
05-03 19:08:21.642: D/dalvikvm(582): GC_FOR_ALLOC freed 120K, 3% free 9333K/9543K, paused 60ms 
05-03 19:08:21.802: D/gralloc_goldfish(582): Emulator without GPU emulation detected. 
05-03 19:08:21.851: I/dalvikvm(582): threadid=3: reacting to signal 3 
05-03 19:08:21.861: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt' 
05-03 19:08:58.581: D/AndroidRuntime(582): Shutting down VM 
05-03 19:08:58.581: W/dalvikvm(582): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
05-03 19:08:58.601: E/AndroidRuntime(582): FATAL EXCEPTION: main 
05-03 19:08:58.601: E/AndroidRuntime(582): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sherlock/com.example.sherlock.Suspectview}: java.lang.IllegalArgumentException: column '_id' does not exist 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.os.Looper.loop(Looper.java:137) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.app.ActivityThread.main(ActivityThread.java:4424) 
05-03 19:08:58.601: E/AndroidRuntime(582): at java.lang.reflect.Method.invokeNative(Native Method) 
05-03 19:08:58.601: E/AndroidRuntime(582): at java.lang.reflect.Method.invoke(Method.java:511) 
05-03 19:08:58.601: E/AndroidRuntime(582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
05-03 19:08:58.601: E/AndroidRuntime(582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
05-03 19:08:58.601: E/AndroidRuntime(582): at dalvik.system.NativeStart.main(Native Method) 
05-03 19:08:58.601: E/AndroidRuntime(582): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.widget.CursorAdapter.init(CursorAdapter.java:168) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.widget.CursorAdapter.<init>(CursorAdapter.java:145) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104) 
05-03 19:08:58.601: E/AndroidRuntime(582): at com.example.sherlock.Suspectview.onCreate(Suspectview.java:42) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.app.Activity.performCreate(Activity.java:4465) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
05-03 19:08:58.601: E/AndroidRuntime(582): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
05-03 19:08:58.601: E/AndroidRuntime(582): ... 11 more 
05-03 19:08:59.001: I/dalvikvm(582): threadid=3: reacting to signal 3 
05-03 19:08:59.012: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt' 
05-03 19:08:59.552: I/dalvikvm(582): threadid=3: reacting to signal 3 
05-03 19:08:59.571: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt' 

DatabaseTable

private static final int DATABASE_VERSION = 1; 

// Database Name 
private static final String DATABASE_NAME = "suspectsManager"; 

// Contacts table name 
private static final String TABLE_SUSPECTS = "suspects"; 

// Contacts Table Columns names 
public static final String KEY_ID = "_id"; 
public static final String KEY_NAME = "name"; 
public static final String KEY_SEX = "sex"; 
public static final String KEY_HEIGHT = "height"; 
public static final String KEY_AGE = "age"; 
public static final String KEY_HAIR = "hair"; 
public static final String KEY_ADD = "additional"; 
public static final String KEY_NAT = "nationality"; 
public static final String KEY_TYPE = "type"; 

而且代碼可以由菱 三江源提前

+0

您是否修改了表來添加KEY_ID? – 2013-05-03 19:30:24

回答

0

更改您KEY_ID = "id"KEY_ID = "_id"和更改DATABASE_VERSION 2或使用系統設置清除應用數據。

+0

更改過程對結果沒有影響 – user2340360 2013-05-03 19:53:54

+0

發佈您的創建TABLE_SUSPECTS代碼 – 2013-05-03 20:05:58

+0

發表以上代碼 – user2340360 2013-05-04 16:25:57