Android类崩溃了,不知道为什么

时间:2012-04-03 13:23:56

标签: android database android-listview listactivity

我正在尝试创建一个将显示数据库内容的类,并且我一直在使用Tutorial来找出执行此操作的方法。在做完教程之后我认为我有一个显示数据库内容的方法。

这是不起作用的课程:

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

}
对不起,我忘了澄清一些事情。一个这个类不会打开,而是崩溃应用程序。这个类的logcat在这里

  

04-03 14:31:25.556:ERROR / Database(14884):在准备'create tableacts时,在0x2c2a00上失败1(接近“tableacts”:语法错误)(id2integer主键自动递增,datetext not null,步骤文本没有null,calories text not null);'。   04-03 14:31:25.556:DEBUG / AndroidRuntime(14884):关闭VM   04-03 14:31:25.556:WARN / dalvikvm(14884):threadid = 1:线程退出时未捕获异常(组= 0x400259f8)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):致命异常:主要   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):java.lang.RuntimeException:无法启动活动ComponentInfo {com.b00348312.workout / com.b00348312.workout.WorkoutProgress}:android.database.sqlite.SQLiteException :near“tableacts”:语法错误:create tableacts(id2integer主键autoincrement,datetext not null,steps text not null,calories text not null);   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在android.app.ActivityThread.access $ 2300(ActivityThread.java:135)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2136)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在android.os.Handler.dispatchMessage(Handler.java:99)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在android.os.Looper.loop(Looper.java:144)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在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):引起:android.database.sqlite.SQLiteException:near“tableacts”:语法错误:create tableacts(id2integer主键autoincrement,datetext not null,steps text not not null,calories text not null);   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在android.database.sqlite.SQLiteDatabase.native_execSQL(本机方法)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在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):在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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)   04-03 14:31:25.566:ERROR / AndroidRuntime(14884):... 11更多

1 个答案:

答案 0 :(得分:3)

在声明数据库创建字符串的字符串中留一些空格(可能在onCreate的{​​{1}}方法中):

id2integer 主键自动增量,日期文字非空,步骤文字不为空,卡路里文字不为空

检查上面的字符串并在SQLiteOpenHelperid2(数据库表的第一列和第二列)之后放置一些空格。