我正在尝试在SQLITE数据库中插入数据,但是当我尝试这样做时,我的应用程序崩溃了 这是我的代码 我试图在最终点击或单选按钮上插入数据。 这是插入块
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
// Log.d("Questions", "Moving to next question");
a++;
/**
* validate a checkbox has been selected
*/
if (!checkAnswer())
return;
/**
* check if end of game
*/
if (currentGame.isGameOver()) {
db.open();
String total = currentGame.getRight() + "";
Log.i("Sanket", total);
db.insertOptions(topic1, total, mon);
db.close();
Intent i = new Intent(this, EndgameActivity.class);
startActivity(i);
a = 0;
finish();
} else {
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
}
这是我的DatabaseAdapter类 我在这里定义了所有方法
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
public class DBAdapter {
static final String topic = "topic";
static final String score = "score";
static final String month = "month";
static final String DATABASE_NAME = "questionDb";
static final String DATABASE_TABLE = "marks";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE = "create table marks"
+ "(topic text,score text,month text);";
final Context context;
DatabseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context context) {
this.context = context;
DBHelper = new DatabseHelper(context);
}
private static class DatabseHelper extends SQLiteOpenHelper {
public DatabseHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.i("Sanket", "Upgrading database version from versin "
+ oldVersion + " to " + newVersion
+ " which may cause to destroy all old data");
db.execSQL("DROP TABLE IF EXISTS marks");
onCreate(db);
}
}
public DBAdapter open() {
// TODO Auto-generated method stub
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
// TODO Auto-generated method stub
DBHelper.close();
}
public long insertOptions(String topic1, String mon,
String total) {
// TODO Auto-generated method stub
ContentValues initialValues = new ContentValues();
initialValues.put(topic, topic1);
initialValues.put(score, mon);
initialValues.put(month, total);
return db.insert(DATABASE_TABLE, null, initialValues);
}
Cursor getAllQuestions() {
// TODO Auto-generated method stub
return db.query(DATABASE_TABLE, new String[] { topic, score, month },
null, null, null, null, null);
}
long getNoOfRows() {
String sql = "SELECT COUNT(*) FROM " + DATABASE_TABLE;
SQLiteStatement statement = db.compileStatement(sql);
long count = statement.simpleQueryForLong();
return count;
}
Cursor getAlldata() {
String selectQuery = ("select * from marks");
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}
}
请仔细阅读我的代码并告知我们所需的更改
这是LOGCAT
02-04 13:06:57.259:E / AndroidRuntime(1114):致命异常:主要 02-04 13:06:57.259:E / AndroidRuntime(1114):java.lang.NullPointerException:println需要一条消息 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.util.Log.println_native(本机方法) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.util.Log.i(Log.java:159) 02-04 13:06:57.259:E / AndroidRuntime(1114):at com.tmm.android.chuck.QuestionActivity.onCheckedChanged(QuestionActivity.java:195) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.widget.RadioGroup.setCheckedId(RadioGroup.java:174) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.widget.RadioGroup.access $ 600(RadioGroup.java:54) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.widget.RadioGroup $ CheckedStateTracker.onCheckedChanged(RadioGroup.java:358) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.widget.CompoundButton.setChecked(CompoundButton.java:129) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.widget.CompoundButton.toggle(CompoundButton.java:87) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.widget.RadioButton.toggle(RadioButton.java:76) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.widget.CompoundButton.performClick(CompoundButton.java:99) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.view.View $ PerformClick.run(View.java:17721) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.os.Handler.handleCallback(Handler.java:730) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.os.Handler.dispatchMessage(Handler.java:92) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.os.Looper.loop(Looper.java:137) 02-04 13:06:57.259:E / AndroidRuntime(1114):在android.app.ActivityThread.main(ActivityThread.java:5103) 02-04 13:06:57.259:E / AndroidRuntime(1114):at java.lang.reflect.Method.invokeNative(Native Method) 02-04 13:06:57.259:E / AndroidRuntime(1114):at java.lang.reflect.Method.invoke(Method.java:525) 02-04 13:06:57.259:E / AndroidRuntime(1114):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 02-04 13:06:57.259:E / AndroidRuntime(1114):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 02-04 13:06:57.259:E / AndroidRuntime(1114):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您尚未在marks
班级
DBAdapter
表格
创建表
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(TABLE_NAME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// create new tables
onCreate(db);
}
答案 1 :(得分:1)
您的数据库处理程序onCreate()
为空。您的数据库没有任何表格。
在那里放置所需的CREATE TABLE
SQL并卸载您的应用程序,以便删除旧的空数据库文件。
您在问题中添加的异常堆栈跟踪“println需要一条消息”来自此处:
String total = currentGame.getRight() + "";
Log.i("Sanket", total);
因为total
是一个空字符串。将日志记录更改为
String total = currentGame.getRight() + "";
Log.i("Sanket", "total: " + total);