RuntimeException:无法恢复 activity

RuntimeException: Unable to resume activity

我是 Android 的新手,正在尝试创建一个包含两个表的数据库,但是当我启动我的项目时,我在 logcat 中遇到以下错误:

 05-04 11:21:29.678    1634-1634/com.example.keco.myapplication E/SQLiteLog﹕ 

(1) near "TABLEstudents": syntax error
05-04 11:21:29.680    1634-1634/com.example.keco.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.keco.myapplication, PID: 1634
    java.lang.RuntimeException: Unable to resume activity {com.example.keco.myapplication/com.example.keco.myapplication.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEstudents": syntax error (code 1): , while compiling: CREATE TABLEstudents(_id INTEGER PRIMARY KEY AUTOINCREMENT, _name VARCHAR(30))
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2951)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.database.sqlite.SQLiteException: near "TABLEstudents": syntax error (code 1): , while compiling: CREATE TABLEstudents(_id INTEGER PRIMARY KEY AUTOINCREMENT, _name VARCHAR(30))
            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.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
            at com.example.keco.myapplication.SqlGenerator.onCreate(SqlGenerator.java:38)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
            at com.example.keco.myapplication.DatabaseSource.<init>(DatabaseSource.java:17)
            at com.example.keco.myapplication.MainActivity.onResume(MainActivity.java:63)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
            at android.app.Activity.performResume(Activity.java:6019)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

这是扩展 SQLiteOpenHelper 的 class:

  package com.example.keco.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

/**
 * Created by Keco on 04/05/2015.
 */
public class SqlGenerator extends SQLiteOpenHelper {
    private static final String STUDENTS_TABLE="students";
    private static final String COLUMN_name="_name";
    private static final String DB_NAME="school.db";
    private static final int DB_VERSION=1;


    public static final String CREATE_STUDENTS_TABLE=
            " CREATE TABLE"+ STUDENTS_TABLE+"("+ BaseColumns._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
                                  COLUMN_name+" VARCHAR(30)) ";



    public SqlGenerator (Context context){
        super(context,DB_NAME,null,DB_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(CREATE_STUDENTS_TABLE);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

为了清理我的代码,我在单独的 class 中声明了 SqlGenerator 对象,如下所示:

package com.example.keco.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by Keco on 04/05/2015.
 */
public class DatabaseSource {

    private Context mContext;
    private SqlGenerator mSqlGenerator;

    public DatabaseSource (Context context){
        mContext=context;
        mSqlGenerator=new SqlGenerator(context);
        SQLiteDatabase database=mSqlGenerator.getReadableDatabase();
        database.close();
    }

}

在 MainActivity 中,我声明并初始化 DatabaseSource 对象如下:

package com.example.keco.myapplication;

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }


    @Override
    protected void onResume() {
        super.onResume();

       DatabaseSource databaseSource=new DatabaseSource(this);

    }


}

等待您的帮助...

如错误消息所示,您的 CREATE TABLE 语句缺少必要的空格

替换

" CREATE TABLE"+ STUDENTS_TABLE+"..."

" CREATE TABLE " + STUDENTS_TABLE + "..."
//            ^

正如它所说,这是一个语法错误:in the CREATE_STUDENTS_TABLE STRING

public class SqlGenerator extends SQLiteOpenHelper {

     [...]

public static final String CREATE_STUDENTS_TABLE=
            " CREATE TABLE"+ STUDENTS_TABLE+"("+ BaseColumns._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
                                  COLUMN_name+" VARCHAR(30)) ";

后应该有一个space
" CREATE TABLE<here>"   
===>   " CREATE TABLE "+ STUDENTS_TABLE+"...