2012-04-03 136 views
1

我想創建一個類,將顯示數據庫的內容,我一直在使用Tutorial找出這樣做的方法。在完成教程後,我想我有一個方法f顯示數據庫的內容。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_TITLE }; 
     int[] to = new int[] { R.id.text1 }; 

     // 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); 
    } 

} 

OK對不起,我忘了澄清一些事情。一個這個類不會打開,而是會崩潰應用程序。這個類的logcat的是這裏

04-03 14:31:25.556: ERROR/Database(14884): Failure 1 (near "tableacts": syntax error) on 0x2c2a00 when preparing 'create tableacts (id2integer primary key autoincrement, datetext not null,steps text not null, calories text not null);'. 04-03 14:31:25.556: DEBUG/AndroidRuntime(14884): Shutting down VM 04-03 14:31:25.556: WARN/dalvikvm(14884): threadid=1: thread exiting with uncaught exception (group=0x400259f8) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): FATAL EXCEPTION: main 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutProgress}: android.database.sqlite.SQLiteException: near "tableacts": syntax error: create tableacts (id2integer primary key autoincrement, datetext not null,steps text not null, calories text not null); 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.app.ActivityThread.access$2300(ActivityThread.java:135) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.os.Handler.dispatchMessage(Handler.java:99) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.os.Looper.loop(Looper.java:144) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.app.ActivityThread.main(ActivityThread.java:4937) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at java.lang.reflect.Method.invokeNative(Native Method) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at java.lang.reflect.Method.invoke(Method.java:521) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at dalvik.system.NativeStart.main(Native Method) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): Caused by: android.database.sqlite.SQLiteException: near "tableacts": syntax error: create tableacts (id2integer primary key autoincrement, datetext not null,steps text not null, calories text not null); 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1817) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at com.b00348312.workout.DataBaseHelper$DatabaseHelper.onCreate(DataBaseHelper.java:60) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at com.b00348312.workout.DataBaseHelper.open(DataBaseHelper.java:78) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at com.b00348312.workout.WorkoutProgress.onCreate(WorkoutProgress.java:25) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751) 04-03 14:31:25.566: ERROR/AndroidRuntime(14884): ... 11 more

+2

請告訴我們什麼是不工作。最好的辦法是在Logcat中找到異常的堆棧跟蹤並將其發佈到此處。 – Janusz 2012-04-03 13:26:58

+0

可以請粘貼Logcat嗎? – Nikhil 2012-04-03 13:28:19

+0

你的創建語句似乎出錯了? – Nanne 2012-04-03 13:36:33

回答

3

留一些空間讓你聲明的數據庫創建的字符串在字符串中(可能在SQLiteOpenHelperonCreate法):

id2integer主鍵自動遞增, datetext不爲空,步驟文本不爲空,熱量文本不爲空

檢查上面的字符串,並把一些空間後和date(數據庫表的第一列和第二列)。