2017-04-13 48 views
-3

我試圖從我的數據庫獲取數據,但是當我這樣做時應用程序崩潰。當我嘗試從數據庫獲取所有項目時,應用程序崩潰

請檢查我的代碼,並幫助我,我試圖解決它,但我找不到有什麼問題。

這就是我試圖挽救:

public class SinglePet { 

private String name; 
private Bitmap photo; 
private String task1; 
private String task2; 
private String task3; 

private long id; 

public SinglePet(String name, Bitmap photo, String task1, String task2, String task3, long id) { 
    this.name = name; 
    this.photo = photo; 
    this.task1 = task1; 
    this.task2 = task2; 
    this.task3 = task3; 
    this.id = id; 
} 

//and getter and setter... 

這是我的數據庫:

公共類MyPetsOpenHelper擴展SQLiteOpenHelper {

public static final String DATABASENAME="pets.db"; 
public static final String TABLE_MYPETS="tblmypets"; 
public static final int DATABASEVERSION=1; 

public static final String COLUMN_ID="id"; 
public static final String COLUMN_IMAGE="image"; 
public static final String COLUMN_NAME="name"; 
public static final String COLUMN_TASK1="task1"; 
public static final String COLUMN_TASK2="task2"; 
public static final String COLUMN_TASK3="task3"; 

private static final String CREATE_TABLE_MYPETS="CREATE TABLE IF NOT EXISTS " + 
     TABLE_MYPETS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " VARCHAR," + COLUMN_IMAGE + " BLOB," 
     + COLUMN_TASK1 + " VARCHAR," + COLUMN_TASK2 + " VARCHAR," + COLUMN_TASK3 + " VARCHAR" + ");"; 

String [] allColumns={MyPetsOpenHelper.COLUMN_ID, MyPetsOpenHelper.COLUMN_NAME, MyPetsOpenHelper.COLUMN_IMAGE, 
     MyPetsOpenHelper.COLUMN_TASK1, MyPetsOpenHelper.COLUMN_TASK2, MyPetsOpenHelper.COLUMN_TASK3}; 

SQLiteDatabase database; 

public MyPetsOpenHelper(Context context) 
{ 
    super(context, DATABASENAME, null, DATABASEVERSION); 
} 

public void onCreate(SQLiteDatabase db) { 
    db.execSQL(CREATE_TABLE_MYPETS); 
    Log.d("data", "Table MyPets is created"); 
} 

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_MYPETS); 
    onCreate(db); 
} 

public void open() 
{ 
    database=this.getWritableDatabase(); 
    Log.d("data", "database connection open"); 
} 

//QUERIES 
public SinglePet createSinglePet(SinglePet s) 
{ 
    ContentValues values= new ContentValues(); 
    values.put(COLUMN_NAME, s.getName()); 
    Bitmap b=s.getPhoto(); 
    values.put(COLUMN_IMAGE,PhotoHelper.bitmapToString(b)); 
    values.put(COLUMN_TASK1, s.getTask1()); 
    values.put(COLUMN_TASK2, s.getTask2()); 
    values.put(COLUMN_TASK3, s.getTask3()); 

    long insertId=database.insert(TABLE_MYPETS, null, values); 
    Log.d("data", "MyPets " + insertId + " insert to database"); 
    s.setId(insertId); 
    return s; 
} 

當我嘗試運行此功能( getAll())應用程序崩潰!

public ArrayList<SinglePet> getAll() 
{ 
    ArrayList<SinglePet> l= new ArrayList<SinglePet>(); 
    Cursor cursor=database.query(MyPetsOpenHelper.TABLE_MYPETS, allColumns, null, null, null, null, null); 
    if(cursor.getCount()>0) 
    { 
     while (cursor.moveToNext()) 
     { 
      long id= cursor.getLong(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_ID)); 
      String name= cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_NAME)); 
      byte [] image= cursor.getBlob(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_IMAGE)); 
      Bitmap imageB= PhotoHelper.byteArrayTBitmap(image); 
      String task1= cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK1)); 
      String task2= cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK2)); 
      String task3= cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK3)); 

      SinglePet s=new SinglePet(name, imageB , task1, task2, task3, id); 
      l.add(s); 
     } 
    } 
    return l; 


// and the rest of the class doesn't matter... 

的問題是,當我寫東西喜歡 -

列表= myPetsOpenHelper.getAll();

該應用程序崩潰了!

PLEASE HELP ME

完整跟蹤:

04-14 15:46:49.913 11231-11231/com.example.admin.petcare I /數據:表大小爲0 04-14 15 :46:49.933 11231-11231/com.example.admin.petcare D/data:數據庫連接打開 04-14 15:46:49.983 11231-11231/com.example.admin.petcare E/SQLiteLog:(1)table tblmypets沒有名爲task2的列 04-14 15:46:49.983 11231-11231/com.example.admin.petcare E/SQLiteDatabase:插入圖像時出錯= [B @ a5a2aa38 task1 = task1 task2 = task2 task3 = task3 name = example android.databa sesqlite.SQLiteException:表tblmypets沒有名爲task2的列(代碼1):,編譯時:INSERT INTO tblmypets(image,task1,task2,task3,name)VALUES(?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31 )在android上的 (SQLiteDatabase.java:1339) at com.example.admin.petcare.MyPetsOpenHelper.createSinglePet(MyPetsOpenHelper.java(SQLiteDatabase.java:1339) :74) at com.example.admin.petcare.MyPetsActivity.examplePet(MyPetsActivity.java:182) at com.example.admin.petcare.MyPetsActivity.onCreate(MyPetsActivity.java:68) at android.app.Activity .performCreate(Activity.java:5133) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 在android.app.ActivityThread。在android.app.ActivityThread.access處理HandlerLaunchActivity(ActivityThread.java:2261) $ 600(ActivityThread.java:141) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) at android.os.Handler .dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method .invokeNative(本地方法) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) at com .android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 在dalvik.system.NativeStart.main(本機方法) 04-14 15:46:49.983 11231-11231/com.example.admin.petcare d /數據:MyPets -1插入到數據庫 04-14 15:46:49.993 11231-11231/com.example.admin.petcare d /數據:數據庫連接開放 04-14 15:46:49.993 11231-11231/com.example.admin.petcare E/SQLiteLog:(1)沒有這樣的柱:TASK2 04-14 15:46:49.993 11231-11231/com.example.admin.petcare d/AndroidRuntime:關閉VM 04-14 15:46:49.993 11231-11231/com.example.admin.petcare W/dalvikvm:線程ID = 1:螺紋與未捕獲的異常退出(組= 0xa4fdc678) 04-14 15:46:50.003 11231-11231/com。示例.admin.petcare E/AndroidRuntime:致命例外:main java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.admin.petcare/com.example.admin.petcare.MyPetsActivity}:android.database.sqlite.SQLiteException:no such column:task2(code 1):,當編譯:SELECT id,name,image,task1,task2,task3 FROM t.t.在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) 在android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main (本機方法) 所致:android.database.sqlite.SQLiteException:沒有這樣的柱:TASK2(碼1):在編譯:SELECT ID,名稱,圖像,任務1,任務2,TASK3 FROM在android.database tblmypets 。 sqlite.SQLiteConnection.nativePrepareStatement(母語方法)at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare( SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver。 query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) at android.database.sqlite。SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) at com。 example.admin.petcare.MyPetsOpenHelper.getAll(MyPetsOpenHelper.java:83) at com.example.admin.petcare.MyPetsActivity.onCreate(MyPetsActivity.java:72) at android.app.Activity.performCreate(Activity.java: 5133) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) at android.app。在android.app.ActivityThread.access $ 600(ActivityThread.java:141) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) at android.os .Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect .Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)W/ActivityManager/at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) 04-14 15:46:50.013 1460-1460/system_process W/ActivityManager :強制整理活動com.example.admin.petcare/.MyPetsActivity 04-14 15:46:50.343 1974-11625/com.google.android.gms D/DropBoxEntryAddedChimeraService:用戶未選擇使用& Diagnostics或Lockbox 。 04-14 15:46:50.523 1460-1477/system_process W/ActivityManager:ActivityRecord的活動暫停超時{a5642da0 u0 com.example.admin.petcare/.MyPetsActivity} 04-14 15:46:59.723 1460-1477/system_process W/ActivityManager:啓動超時已過,放棄喚醒鎖定! 04-14 15:46:59.743 1460-1477/system_process W/ActivityManager:ActivityRecord的活動空閒超時{a5642da0 u0 com.example.admin.petcare/.MyPetsActivity} 04-14 15:47:00.533 1460-1477/system_process W/ActivityManager:ActivityRecord的活動空閒超時{a54c27e0 u0 com.example.admin.petcare/.MainActivity} 04-14 15:47:04.733 1460-1477/system_process W/ProcessStats:跳過未知進程pid 11730 04- 14 15:47:09.753 1460-1477/system_process W/ActivityManager:ActivityRecord的活動銷燬超時{a5642da0 u0 com.example.admin.petcare/.MyPetsActivity} 04-14 15:48:19.403 1460-1477/system_process W/BroadcastQueue:廣播超時時間BroadcastRecord {a572c658 u0 com.google.firebase.INSTANCE_ID_EVENT} - [email protected],已啓動60021ms ago 04-14 15:48:1940 14 60-1477/system_process W/BroadcastQueue:超時期間的接收方:ResolveInfo {a572cb00 com.google.firebase.iid.FirebaseInstanceIdInternalReceiver p = 0 o = 0 m = 0x0} 04-14 15:48:19.433 1460-1477/system_process I/ActivityManager:崩潰應用程序跳過ANR:ProcessRecord {a5637df0 11231:com.example.admin.petcare/u0a10050}廣播的意圖{act = com.google.firebase.INSTANCE_ID_EVENT flg = 0x14 cmp = com.example.admin.petcare/com .google.firebase.iid.FirebaseInstanceIdInternalReceiver(有額外功能)}

+0

寫你的調用堆棧 – Vyacheslav

+0

請發佈你的崩潰日誌 –

+0

做到了(我編輯了我的帖子) – Meow

回答

0

我無法找到你的日誌顯示的錯誤的原因(你的代碼看起來很好)。雖然,我測試了你的代碼並做了一些修改(與錯誤無關)。它工作正常。您可以檢查差異並相應地修改您自己的代碼,或只複製下面的代碼。這裏是我的MyPetsOpenHelper.java代碼。 (您提供的部分)。

public class MyPetsOpenHelper extends SQLiteOpenHelper { 

    public static final String DATABASENAME = "pets.db"; 
    public static final String TABLE_MYPETS = "tblmypets"; 
    public static final int DATABASEVERSION = 2; 

    public static final String COLUMN_ID = "id"; 
    public static final String COLUMN_IMAGE = "image"; 
    public static final String COLUMN_NAME = "name"; 
    public static final String COLUMN_TASK1 = "task1"; 
    public static final String COLUMN_TASK2 = "task2"; 
    public static final String COLUMN_TASK3 = "task3"; 

    private static final String CREATE_TABLE_MYPETS = "CREATE TABLE IF NOT EXISTS " + 
      TABLE_MYPETS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " VARCHAR," + COLUMN_IMAGE + " BLOB," 
      + COLUMN_TASK1 + " VARCHAR," + COLUMN_TASK2 + " VARCHAR," + COLUMN_TASK3 + " VARCHAR" + ");"; 

    String[] allColumns = {MyPetsOpenHelper.COLUMN_ID, MyPetsOpenHelper.COLUMN_NAME, MyPetsOpenHelper.COLUMN_IMAGE, 
      MyPetsOpenHelper.COLUMN_TASK1, MyPetsOpenHelper.COLUMN_TASK2, MyPetsOpenHelper.COLUMN_TASK3}; 

    SQLiteDatabase database; 

    public MyPetsOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
     super(context, DATABASENAME, factory, DATABASEVERSION); 
    } 

    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_TABLE_MYPETS); 
     Log.d("data", "Table MyPets is created"); 
    } 

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS" + TABLE_MYPETS); 
     onCreate(db); 
    } 

    public void open() { 
     database = this.getWritableDatabase(); 
     Log.d("data", "database connection open"); 
    } 

    public void close() { 
     database.close(); 
     Log.d("data", "database connection closed"); 
    } 

    //QUERIES 
    public SinglePet createSinglePet(SinglePet s) { 
     ContentValues values = new ContentValues(); 
     open(); 
     values.put(COLUMN_NAME, s.getName()); 
      Bitmap b = s.getPhoto(); 
      values.put(COLUMN_IMAGE, PhotoHelper.bitmapToString(b)); 
     values.put(COLUMN_TASK1, s.getTask1()); 
     values.put(COLUMN_TASK2, s.getTask2()); 
     values.put(COLUMN_TASK3, s.getTask3()); 

     long insertId = database.insert(TABLE_MYPETS, null, values); 
     Log.d("data", "MyPets " + insertId + " insert to database"); 
     s.setId(insertId); 
     close(); 
     return s; 
    } 

    public ArrayList<SinglePet> getAll() { 
     ArrayList<SinglePet> l = new ArrayList<SinglePet>(); 
     open(); 
     Cursor cursor = database.query(MyPetsOpenHelper.TABLE_MYPETS, allColumns, null, null, null, null, null); 
     if (cursor != null) { 
      cursor.moveToFirst(); 
      while (!cursor.isAfterLast()) { 
       long id = cursor.getLong(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_ID)); 
       String name = cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_NAME)); 
       byte [] image= cursor.getBlob(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_IMAGE)); 
       Bitmap imageB= PhotoHelper.byteArrayTBitmap(image); 
       String task1 = cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK1)); 
       String task2 = cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK2)); 
       String task3 = cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK3)); 

       SinglePet s = new SinglePet(name, task1, task2, task3, id); 
       l.add(s); 
       cursor.moveToNext(); 
      } 
      cursor.close(); 
      close(); 
     } 
     return l; 

    } 
} 

注:我已經改變了DATABASEVERSION爲2將重新創建數據庫,因此以前的表和數據都將被刪除。所以你必須重新輸入你的數據。希望這可以幫助。

+0

它仍然崩潰了.. – Meow

+0

好吧。謝謝 – Meow

+0

不幸的是沒有.. – Meow

相關問題