加载活动和应用程序崩溃时出错

时间:2014-02-18 18:30:29

标签: java android

当我尝试启动活动时,它会崩溃,我无法找出原因

最新情况,活动不会加载

Log Cat:

02-18 18:23:27.614: D/dalvikvm(330): GC_EXTERNAL_ALLOC freed 50K, 53% free 2553K/5379K, external 2730K/3266K, paused 74ms
02-18 18:23:28.504: D/dalvikvm(330): GC_EXTERNAL_ALLOC freed 1K, 53% free 2552K/5379K, external 6764K/8447K, paused 57ms
02-18 18:23:43.344: D/dalvikvm(330): GC_EXTERNAL_ALLOC freed 11K, 52% free 2583K/5379K, external 8695K/10350K, paused 48ms
02-18 18:23:43.664: I/Database(330): sqlite returned: error code = 1, msg = near "TABLETotal_Wins": syntax error
02-18 18:23:43.674: E/Database(330): Failure 1 (near "TABLETotal_Wins": syntax error) on 0x3b1788 when preparing 'CREATE TABLETotal_Wins (_id INTEGER PRIMARY KEY AUTOINCREMENT, number_of_wins INTEGER);'.
02-18 18:23:43.684: D/AndroidRuntime(330): Shutting down VM
02-18 18:23:43.684: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-18 18:23:43.704: E/AndroidRuntime(330): FATAL EXCEPTION: main
02-18 18:23:43.704: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.connectfour/com.example.connectfour.ScoreActivity}: android.database.sqlite.SQLiteException: near "TABLETotal_Wins": syntax error: CREATE TABLETotal_Wins (_id INTEGER PRIMARY KEY AUTOINCREMENT, number_of_wins INTEGER);
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.os.Looper.loop(Looper.java:123)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 18:23:43.704: E/AndroidRuntime(330):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 18:23:43.704: E/AndroidRuntime(330):  at java.lang.reflect.Method.invoke(Method.java:507)
02-18 18:23:43.704: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 18:23:43.704: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 18:23:43.704: E/AndroidRuntime(330):  at dalvik.system.NativeStart.main(Native Method)
02-18 18:23:43.704: E/AndroidRuntime(330): Caused by: android.database.sqlite.SQLiteException: near "TABLETotal_Wins": syntax error: CREATE TABLETotal_Wins (_id INTEGER PRIMARY KEY AUTOINCREMENT, number_of_wins INTEGER);
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
02-18 18:23:43.704: E/AndroidRuntime(330):  at com.example.connectfour.Database$DBHelper.onCreate(Database.java:29)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
02-18 18:23:43.704: E/AndroidRuntime(330):  at com.example.connectfour.Database.open(Database.java:48)
02-18 18:23:43.704: E/AndroidRuntime(330):  at com.example.connectfour.ScoreActivity$MyCursorAdapter.<init>(ScoreActivity.java:20)
02-18 18:23:43.704: E/AndroidRuntime(330):  at com.example.connectfour.ScoreActivity.onCreate(ScoreActivity.java:48)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 18:23:43.704: E/AndroidRuntime(330):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 18:23:43.704: E/AndroidRuntime(330):  ... 11 more
02-18 18:23:53.828: I/Process(330): Sending signal. PID: 330 SIG: 9
02-18 18:25:14.244: D/dalvikvm(362): GC_EXTERNAL_ALLOC freed 50K, 53% free 2553K/5379K, external 2730K/3266K, paused 163ms
02-18 18:25:15.014: D/dalvikvm(362): GC_EXTERNAL_ALLOC freed 1K, 53% free 2552K/5379K, external 6764K/8447K, paused 45ms
02-18 18:26:36.184: D/dalvikvm(362): GC_EXTERNAL_ALLOC freed 11K, 52% free 2583K/5379K, external 8695K/10350K, paused 49ms
02-18 18:26:36.504: I/Database(362): sqlite returned: error code = 1, msg = near "TABLETotal_Wins": syntax error
02-18 18:26:36.504: E/Database(362): Failure 1 (near "TABLETotal_Wins": syntax error) on 0x3b17b8 when preparing 'CREATE TABLETotal_Wins (_id INTEGER PRIMARY KEY AUTOINCREMENT, number_of_wins INTEGER);'.
02-18 18:26:36.534: D/AndroidRuntime(362): Shutting down VM
02-18 18:26:36.534: W/dalvikvm(362): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-18 18:26:36.564: E/AndroidRuntime(362): FATAL EXCEPTION: main
02-18 18:26:36.564: E/AndroidRuntime(362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.connectfour/com.example.connectfour.ScoreActivity}: android.database.sqlite.SQLiteException: near "TABLETotal_Wins": syntax error: CREATE TABLETotal_Wins (_id INTEGER PRIMARY KEY AUTOINCREMENT, number_of_wins INTEGER);
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.os.Looper.loop(Looper.java:123)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 18:26:36.564: E/AndroidRuntime(362):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 18:26:36.564: E/AndroidRuntime(362):  at java.lang.reflect.Method.invoke(Method.java:507)
02-18 18:26:36.564: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 18:26:36.564: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 18:26:36.564: E/AndroidRuntime(362):  at dalvik.system.NativeStart.main(Native Method)
02-18 18:26:36.564: E/AndroidRuntime(362): Caused by: android.database.sqlite.SQLiteException: near "TABLETotal_Wins": syntax error: CREATE TABLETotal_Wins (_id INTEGER PRIMARY KEY AUTOINCREMENT, number_of_wins INTEGER);
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
02-18 18:26:36.564: E/AndroidRuntime(362):  at com.example.connectfour.Database$DBHelper.onCreate(Database.java:29)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
02-18 18:26:36.564: E/AndroidRuntime(362):  at com.example.connectfour.Database.open(Database.java:48)
02-18 18:26:36.564: E/AndroidRuntime(362):  at com.example.connectfour.ScoreActivity$MyCursorAdapter.<init>(ScoreActivity.java:20)
02-18 18:26:36.564: E/AndroidRuntime(362):  at com.example.connectfour.ScoreActivity.onCreate(ScoreActivity.java:48)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 18:26:36.564: E/AndroidRuntime(362):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 18:26:36.564: E/AndroidRuntime(362):  ... 11 more

数据库类:

public class Database {

public static final String KEY_ROWID = "_id"; 
public static final String KEY_NAME = "number_of_wins"; 

private static final String DATABASE_NAME = "HIGHSCORES"; 
private static final String DATABASE_TABLE= "Total_Wins"; 
private static final int DATABASE_VERSION = 1;

private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE" + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " INTEGER);"
                );
    }

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

}
public Database(Context context) {
    ourContext = context;
}

public Database open() {
    ourHelper = new DBHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public Cursor getScores() {
    return ourDatabase.rawQuery("SELECT * FROM "+DATABASE_TABLE, null); //No Parameter
}

}

ScoreActivity

  public class ScoreActivity extends Activity {

public class MyCursorAdapter extends android.widget.CursorAdapter {

    private int scoreIdx;

    public MyCursorAdapter(Context context) {
        super(context, new Database(context).open().getScores());
        scoreIdx = getCursor().getColumnIndex(Database.KEY_NAME);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ((TextView) view).setText(Integer.toString(cursor.getInt(scoreIdx)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        TextView view = new TextView(context);
        bindView(view, context, cursor);
        return view;
    }

}

Database scoresDatabase = new Database(this);
private ListView listView;

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

    listView = (ListView) findViewById(R.id.highScoreList);
    listView.setAdapter(new MyCursorAdapter(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.leader_board, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:0)

您忘记在查询中CREATE TABLEDATABASE_TABLE之间添加空格,因此请将其添加为:

   db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_NAME + " INTEGER);"
            );

答案 1 :(得分:0)

此代码行中缺少空格:

// Insert space after CREATE TABLE
db.execSQL("CREATE TABLE" + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_NAME + " INTEGER);"
            );

这是您看到的语法错误