2012-04-06 91 views
1

我想運行一個類,它將檢查數據庫中的信息,然後將其顯示在屏幕上。這是工作昨天,直到我在代碼中增加了它的第二個表查資料,我將發佈兩個密碼和logcat的,因爲我不知道爲什麼會這樣Android類不工作

public class WorkoutProgress extends ListActivity { 

    private DataBaseHelper datasource; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    /*requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);*/ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.progress); 

    datasource = new DataBaseHelper(this); 
    datasource.open(); 
    fillData(); 
    datasource.close(); 
    } 
    private void fillData() { 
    // Get all of the notes from the database and create the item list 
    Cursor c = datasource.getAllTitles(); 
    startManagingCursor(c); 

    String[] from = new String[] {DataBaseHelper.KEY_CODE, DataBaseHelper.KEY_DAYS,DataBaseHelper.KEY_BMI }; 
    int[] to = { R.id.code, R.id.Days, R.id.BMI }; 
    String[] from2 = new String[] {DataBaseHelper.KEY_DATE, DataBaseHelper.KEY_STEPS,DataBaseHelper.KEY_CALs }; 
    int[] to2 = { R.id.date, R.id.steps, R.id.cals }; 
    // Now create an array adapter and set it to display using our row 
    SimpleCursorAdapter notes = 
    new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to); 
    setListAdapter(notes); 
    SimpleCursorAdapter notes2 = 
     new SimpleCursorAdapter(this, R.layout.notes_row, c, from2, to2); 
    setListAdapter(notes); 
    } 

    public void add(View view) { 
    //Do nothing 
    } 

    public void delete(View view) { 
    datasource.open(); 
    datasource.deleteFirst(); 
    fillData(); 
    datasource.close(); 
    } 
} 

和日誌貓當我嘗試訪問這個類是

04-06 01:10:42.293: E/global(22219): java.lang.UnsupportedOperationException 
04-06 01:10:42.293: E/global(22219): at java.lang.VMThread.stop(VMThread.java:85) 
04-06 01:10:42.293: E/global(22219): at java.lang.Thread.stop(Thread.java:1391) 
04-06 01:10:42.293: E/global(22219): at java.lang.Thread.stop(Thread.java:1356) 
04-06 01:10:42.293: E/global(22219): at com.b00348312.workout.Splashscreen$1.run(Splashscreen.java:42) 
04-06 01:10:45.393: I/Database(22219): sqlite returned: error code = 1, msg = AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY 
04-06 01:10:45.393: E/Database(22219): Failure 1 (AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY) on 0x2c0e38 when preparing 'create table goals (_id primary key autoincrement, code text not null,days title text not null, bmi text not null);'. 
04-06 01:10:45.403: D/AndroidRuntime(22219): Shutting down VM 
04-06 01:10:45.403: W/dalvikvm(22219): threadid=1: thread exiting with uncaught exception (group=0x400259f8) 
04-06 01:10:45.453: D/dalvikvm(22219): GC_FOR_MALLOC freed 4012 objects/253248 bytes in 32ms 
04-06 01:10:45.453: E/AndroidRuntime(22219): FATAL EXCEPTION: main 
04-06 01:10:45.453: E/AndroidRuntime(22219): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutProgress}: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: create table goals (_id primary key autoincrement, code text not null,days title text not null, bmi text not null); 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.app.ActivityThread.access$2300(ActivityThread.java:135) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.os.Looper.loop(Looper.java:144) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.app.ActivityThread.main(ActivityThread.java:4937) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at java.lang.reflect.Method.invokeNative(Native Method) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at java.lang.reflect.Method.invoke(Method.java:521) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at dalvik.system.NativeStart.main(Native Method) 
04-06 01:10:45.453: E/AndroidRuntime(22219): Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: create table goals (_id primary key autoincrement, code text not null,days title text not null, bmi text not null); 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1817) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at com.b00348312.workout.DataBaseHelper$DatabaseHelper.onCreate(DataBaseHelper.java:59) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at com.b00348312.workout.DataBaseHelper.open(DataBaseHelper.java:78) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at com.b00348312.workout.WorkoutProgress.onCreate(WorkoutProgress.java:21) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) 
04-06 01:10:45.453: E/AndroidRuntime(22219): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751) 
04-06 01:10:45.453: E/AndroidRuntime(22219): ... 11 more 

回答

3

堆棧跟蹤說:

失敗準備1「創建表的目標時,對 0x2c0e38(AUTOINCREMENT只允許上INTEGER PRIMARY KEY) s(_id主鍵 自動增量,代碼文本不爲空,天標題文本不爲空,bmi文本 不爲空);'。

看起來你忘了申報你的create table聲明_id域作爲一個INTEGER

+1

+1。我真的希望新的程序員能夠學會實際閱讀**錯誤信息,而不僅僅是「我的代碼不工作,這是錯誤信息,怎麼了?這個真的很清楚。 – 2012-04-06 00:36:18

+0

@ Ken white,很抱歉,我還不熟悉logcat。我只是真的開始使用它,我不知道我應該尋找哪些錯誤。現在實施這些改變後,我仍然有一個問題,但它的說法是,當我已經明確地定義它時, – 2012-04-06 00:56:59

0

您的DataBaseHelper可能不會指定數據庫創建時的列_ID的類型。您必須將其定義爲INTEGER

0

我剛開始使用Android編程,並使用USB調試來試用我的應用程序。我意識到,在SQL可能發生故障後,下次運行數據庫時不會從設備上清除數據庫,並且所做的任何更改都不會生效。

所以說你的「創建表」SQL語句在某種程度上是錯誤的。您的程序崩潰。你發現問題並修復它。如果再次運行程序,除非增加版本,否則將使用相同的舊數據庫。

因此,請轉到您的設備並卸載該程序,然後再從計算機上運行該程序。這一次,我認爲數據庫將使用全新的「create table」SQL重新創建。

由於我是新人,這可能不是正在發生的事情,如果是這樣,我可以改正。