我有一个带谷歌地图和标记的Andorid应用程序。如果您点击一个标记,您将进入"主屏幕"您可以使用Button开始测验的位置。在完成5个多项选择题后,将显示分数(1 - 5分)。为了这一点,everthing工作。 现在我想将分数保存在SQLite数据库中并在"主屏幕上更改图像"如果得分是5。 现在每次我测试并点击Marker以打开特定位置的"主屏幕"(如果Score = 5,则应该更改图像)应用程序崩溃。 到目前为止,表格中没有数据,因为我从未通过测验来保存分数。如果有人发现错误,我会非常感激!
这是我的代码:
数据库助手:
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 7;
private static final String DATABASE_NAME = "CE";
public static final String SCORE_TABLE = "score";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_SCORE = "SCORE";
public static final String COLUMN_MARKERID = "MARKERID";
private SQLiteDatabase dbase;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//Hier alle Tables erstellen
String create_query = "CREATE TABLE IF NOT EXITS " + SCORE_TABLE + " ( "
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_SCORE + " INTEGER, "
+ COLUMN_MARKERID + " TEXT) ";
db.execSQL(create_query);
}
public void addScore (DbHelper dbh, Integer score, String markerID) {
dbase = dbh.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_SCORE, score);
cv.put(COLUMN_MARKERID, markerID);
dbase.insert(SCORE_TABLE, null, cv);
}
public Cursor getScore(DbHelper dbh) {
dbase = dbh.getReadableDatabase();
String columns[] = {COLUMN_SCORE, COLUMN_MARKERID};
Cursor cursor = dbase.query(SCORE_TABLE, columns, null, null, null, null, null);
return cursor;
}
在完成ResultActivity中的测验后将分数写入数据库:
public class ResultActivity extends Activity {
String markerID;
int score;
TextView t=(TextView)findViewById(R.id.textResult);
Button saveButton = (Button) findViewById(R.id.saveButton);
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_layout);
Bundle b = getIntent().getExtras();
score = b.getInt("score");
markerID = b.getString("markerID");
}
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DbHelper dbh = new DbHelper(context);
dbh.addScore(dbh,score,markerID);
}
});
}
查看"主屏幕"该标记上保存了什么分数:
public class Discover extends ActionBarActivity {
TextView InfoHeadline;
ImageView ImageDone;
Button QuizButton;
String markerID;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.discover_layout);
InfoHeadline = (TextView)findViewById(R.id.InfoUeberschrift);
ImageDone =(ImageView)findViewById(R.id.imageDone);
QuizButton = (Button)findViewById(R.id.QuizButton);
//get markerID from previous google maps activity
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
markerID= null;
} else {
markerID= extras.getString("MarkerID");
}
} else {
markerID = (String) savedInstanceState.getSerializable("MarkerID");
}
InfoHeadline.setText(markerID);
DbHelper dbh = new DbHelper(context);
Cursor cursor = dbh.getScore(dbh);
cursor.moveToFirst();
do {
if (Integer.parseInt(cursor.getString(0))== 5 && InfoHeadline.toString().equals(cursor.getString(1))){
ImageDone.setImageResource(R.drawable.markerdone);
}
}while(cursor.moveToNext());
}
public void StartQuiz (View v) {
Intent intent = new Intent(Discover.this,QuizActivity.class);
intent.putExtra("MarkerID", markerID);
startActivity(intent);
}
}
以下是崩溃报告:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.marmor.discover/de.marmor.discover.Discover}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2407)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5321)
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:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at de.marmor.discover.DbHelper.getScore(DbHelper.java:71)
at de.marmor.discover.Discover.onCreate(Discover.java:46)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)`
...
答案 0 :(得分:3)
context
中的Discover
未初始化,并且您已将null
的上下文传递给DbHelper
。
由于Discover
是Activity
而Activity
是Context
,因此您可以使用this
代替context
。< / p>
答案 1 :(得分:2)
context=null
DbHelper dbh = new DbHelper(context);
初始化DbHelper
喜欢
DbHelper dbh = new DbHelper(getApplicationContext());