Android应用程序在启动运行时崩溃异常
我在代码中没有任何错误,但是当我运行应用程序时它崩溃。这是一张日志图片。 http://postimg.org/image/bbcovjovn/我无法弄清楚代码中有什么问题。Android应用程序在启动运行时崩溃异常
MyBowlingScoresApplication.java
import java.util.ArrayList;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
public class MyBowlingScoresApplication extends Application {
private ArrayList<BowlingScores> allBowlingScores;
private SQLiteDatabase bowlingScoresDB;
@Override
public void onCreate() {
super.onCreate();
BowlingScoresDatabaseHelper databaseHelper1 = new BowlingScoresDatabaseHelper(this);
bowlingScoresDB = databaseHelper1.getWritableDatabase();
//TODO get the data out of the database
allBowlingScores = new ArrayList<BowlingScores>();
}
public void addBowlingScores(BowlingScores bowlingScores){
assert bowlingScores != null;
allBowlingScores.add(bowlingScores);
}
public ArrayList<BowlingScores> getAllBowlingScores() {
return allBowlingScores;
}
private void setAllBowlingScores(ArrayList<BowlingScores> allBowlingScores) {
this.allBowlingScores = allBowlingScores;
}
}
BowlingScoresDatabaseHelper.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class BowlingScoresDatabaseHelper extends SQLiteOpenHelper {
public static final int DB_VERSION = 1;
public static final String DB_NAME = "MyBowlingScores.SQLite";
public static String BOWLING_SCORES_TABLE="BowlingScoresTable";
public static String RECORD_ID="ID";
public static String DATE = "Date";
public static String GAME1 = "Game1";
public static String GAME2 = "Game2";
public static String GAME3 = "Game3";
public BowlingScoresDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase bowlingScoresDB) {
String sqlStatement = "create table " + BOWLING_SCORES_TABLE
+ " ("
+ RECORD_ID + " integer primary key autoincrement not null,"
+ DATE + " integer,"
+ GAME1 + " integer,"
+ GAME2 + " integer,"
+ GAME3 + " integer,"
+ ");";
Log.d("Bowling Database", sqlStatement);
bowlingScoresDB.execSQL(sqlStatement);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
SQLite的查询语法是错误的:
create tableBowlingScoresTable...
应该是这样的:
create table BowlingScoresTable...
忘记空间?
编辑:
好的,您现在已经提供了代码。我看到的另一个语法错误是:
+ GAME3 + " integer,"
最后不应该有逗号,因为后面跟着)
。
我不认为这是问题。我更新上面,因为我不小心贴同班两次我的代码。 – dfasfsd 2014-10-18 18:01:38
@dfasfsd这确实是一个问题!它仍然崩溃? – 2014-10-18 18:33:14
我建议getting familiar with Android's wide set of debugging tools。你本来可以很容易地通过检查堆栈跟踪在logcat中,或查看调用层级时,当调试器附着发生崩溃找准你的问题的原因。
堆栈跟踪清楚地说明了用于创建表的SQL语句包含无效的语法。我发现了两个问题:
- A之后“创建表”丢失的空间,
- 最后一列定义后,一个意外的逗号。
发表您的SQLite数据库的辅助类 – Elltz 2014-10-18 17:31:11
您已在数据库创建粘贴两个类相同......请发表您的BowlingScoresDatabaseHelper类 – Jamil 2014-10-18 17:31:29
请发表您的logcat(不是图片,而是实际的logcat) – 323go 2014-10-18 17:32:14