2011-07-02 55 views
0

我的活動保持力,因爲這類異常關閉的強制關閉我acitivty保留,因爲這個錯誤

07-02 01:24:59.244: ERROR/AndroidRuntime(660): 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fttech.collection/com.fttech.collection.book_list}: 
java.lang.IllegalArgumentException: column '_id' does not exist 

thsi是我的課

public class bookDbHelper{ 

    private static final String DATABASE_NAME = "data"; 
    private static final String DATABASE_TABLE = "books"; 
    private static final int DATABASE_VERSION = 1; 


    public static final String KEY_BOOKTITLE = "title"; 
    public static final String KEY_AUTHOR = "author"; 
    public static final String KEY_ISBN = "isbn"; 
    public static final String KEY_RATING = "rating"; 
    public static final String KEY_ROWID = "_ID"; 


    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 

    private static final String DATABASE_CREATE = 
     " create table " + DATABASE_TABLE + " (" 
     + KEY_ROWID + " integer primary key autoincrement, " 
     + KEY_BOOKTITLE + " text not null, " 
     + KEY_AUTHOR + " text not null, " 
     + KEY_ISBN + " text not null, " 
     + KEY_RATING + " text not null);"; 

    private final Context mCtx; 

    public bookDbHelper(Context ctx){ 
     this.mCtx = ctx; 
    } 
    private static class DatabaseHelper extends SQLiteOpenHelper{ 
     DatabaseHelper(Context context){ 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 

     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(DATABASE_CREATE); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
      // TODO Auto-generated method stub 

     } 
    } 


     public bookDbHelper open()throws SQLException{ 
     mDbHelper = new DatabaseHelper(mCtx); 
     mDb = mDbHelper.getWritableDatabase(); 

     return this; 
    } 

     public void close(){ 
      mDbHelper.close(); 

     } 
     public long addBook(String book_name, String author, String isbn, String rating){ 

      ContentValues initialValues = new ContentValues(); 
      initialValues.put(KEY_BOOKTITLE, book_name); 
      initialValues.put(KEY_AUTHOR, author); 
      initialValues.put(KEY_ISBN, isbn); 
      initialValues.put(KEY_RATING, rating); 

      return mDb.insert(DATABASE_TABLE, null, initialValues); 


     } 
     public boolean DeleteBook(long rowId){ 
      return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 


     } 
     public Cursor fetchAllBooks(){ 
      return mDb.query(DATABASE_TABLE, new String []{KEY_BOOKTITLE, KEY_AUTHOR, KEY_ISBN, KEY_RATING, KEY_ROWID}, null, null, null, null, null); 
     } 
     public Cursor fetchBook(long rowId)throws SQLException { 
      Cursor mCursor = 
       mDb.query(true, DATABASE_TABLE, new String[] {KEY_BOOKTITLE, KEY_AUTHOR, KEY_RATING, KEY_ISBN, KEY_ROWID}, KEY_ROWID + "=" + rowId, null, null, null, null, null); 
      if(mCursor != null){ 
       mCursor.moveToFirst(); 
      } 
      return mCursor; 

} 
     public boolean updateBooks(long rowId, String book_name, String author, String rating, String isbn){ 
      ContentValues args = new ContentValues(); 
      args.put(KEY_BOOKTITLE, book_name); 
      args.put(KEY_AUTHOR, author); 
      args.put(KEY_ISBN, isbn); 
      args.put(KEY_RATING, rating); 
      return 
      mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) >0; 





     } 
} 

這是我從拉數據第二類SQLbase

public class book_list extends ListActivity{  

    private static final int ACTIVITY_CREATE = 0; 
    private static final int ACTIVTY_EDIT = 1; 

    private bookDbHelper mDbHelper; 
      @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.book_list); 
     mDbHelper = new bookDbHelper(this);   
     mDbHelper.open(); 
     fillData(); 
     registerForContextMenu(getListView()); 
    } 

    private void fillData() { 
     Cursor booksCursor = mDbHelper.fetchAllBooks(); 
     startManagingCursor(booksCursor); 

     String [] from = new String[]{bookDbHelper.KEY_BOOKTITLE}; 

     int[] to = new int[]{R.id.text1}; 

     SimpleCursorAdapter books = 
      new SimpleCursorAdapter(this, R.layout.book_row, booksCursor, from, to); 
     setListAdapter(books); 

    } 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id){ 
     super.onListItemClick(l, v, position, id); 

     Intent i = new Intent(this, book_edit.class); 
     i.putExtra(bookDbHelper.KEY_ROWID, id); 
     startActivityForResult(i, ACTIVTY_EDIT); 

    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){ 
     super.onActivityResult(requestCode, resultCode, intent); 
     fillData(); 
    } 

} 
    public class book_list extends ListActivity{ 
    private static final int ACTIVITY_CREATE = 0; 
    private static final int ACTIVTY_EDIT = 1; 

    private bookDbHelper mDbHelper; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.book_list); 
     mDbHelper = new bookDbHelper(this); 
     mDbHelper.open(); 
     fillData(); 
     registerForContextMenu(getListView()); 

    } 

    private void fillData() { 
     Cursor booksCursor = mDbHelper.fetchAllBooks(); 
     startManagingCursor(booksCursor); 

     String [] from = new String[]{bookDbHelper.KEY_BOOKTITLE}; 

     int[] to = new int[]{R.id.text1}; 

     SimpleCursorAdapter books = 
      new SimpleCursorAdapter(this, R.layout.book_row, booksCursor, from, to); 
     setListAdapter(books); 

    } 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id){ 
     super.onListItemClick(l, v, position, id); 

     Intent i = new Intent(this, book_edit.class); 
     i.putExtra(bookDbHelper.KEY_ROWID, id); 
     startActivityForResult(i, ACTIVTY_EDIT); 
    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){ 
     super.onActivityResult(requestCode, resultCode, intent); 
     fillData(); 
    } 

} 
+0

請花時間正確格式化您的代碼(使用編輯器中的「{}」按鈕) 。 –

+0

java.lang.IllegalArgumentException:列'_id'不存在這表明問題..你試圖使用_id列不可用 – Android

回答

0

也許

public static final String KEY_ROWID = "_ID"; 

必須是小寫

public static final String KEY_ROWID = "_id"; 
+0

是的,我只是試圖....沒有工作..我不知道它是什麼是... –

0

我建議你下載的數據庫文件到您的計算機:

adb pull /data/data/[your.application.package.name]/databases/[your.database.name] . 

,然後使用類似SQLite Manager看看數據庫本身。有可能數據庫是使用一些早期版本的代碼創建的,並且從未更新過正確的列,例如...

+0

我在哪裏可以找到數據庫文件? –

+0

這就是答案。如果您到達shell(adb shell),請轉到/data/data/[your.package.name]/databases – Matthieu