Hallo, ich möchte zu einem Spiel eine Highscore Liste anlegen mit einer Datenbank.
Name und score werden an ein Intent angehängt und an eine neue Activity übergeben. Diese sieht folgendermaßen aus:
import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Highscore extends Activity {
// Constants
public static final String DATABASE_NAME = "highscores.db";
public static final String HIGH_SCORE_TABLE = "highscore";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_SCORE = "SCORE";
public static final String COLUMN_NAME = "NAME";
int score=0;
String name="";
private SQLiteDatabase scoreDB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
final WebView webview = (WebView) findViewById(R.id.webView1);
Bundle b = getIntent().getExtras();
score = b.getInt("spielstand");
name=b.getString("spielername");
// Add the values
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_SCORE, score);
scoreDB.insert(HIGH_SCORE_TABLE, null, values);
// Retrieve the new list of scores
Cursor c = scoreDB.query(HIGH_SCORE_TABLE, new String[] {
COLUMN_NAME, COLUMN_SCORE }, null, null, null, null,
COLUMN_SCORE);
StringBuilder builder = new StringBuilder();
builder.append("<html><body><h1>High Scores</h1><table>");
c.moveToLast();
for(int i=c.getCount()-1; i>=0; i--) {
// Get the data
builder.append("<tr><td>");
builder.append(c.getString(0));
builder.append("</td><td>");
builder.append(c.getString(1));
builder.append("</td></tr>");
// Move the cursor
c.moveToPrevious();
}
builder.append("</table></html>");
webview.loadData(builder.toString(), "text/html", "UTF-8");
// Close the cursor
c.close();
}
@Override
protected void onResume() {
super.onResume();
scoreDB = openOrCreateDatabase(DATABASE_NAME,
SQLiteDatabase.CREATE_IF_NECESSARY
| SQLiteDatabase.OPEN_READWRITE, null);
scoreDB.execSQL("CREATE TABLE IF NOT EXISTS " + HIGH_SCORE_TABLE + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY, " + COLUMN_NAME
+ " VARCHAR, " + COLUMN_SCORE + " INT)");
}
@Override
protected void onPause() {
super.onPause();
if (scoreDB.isOpen()) {
scoreDB.close();
}
}
}
Alles anzeigen
Allerdings startet die neue Activity nicht und ich bekomme folgende Fehlermeldungen
06-30 13:17:07.080: E/AndroidRuntime(1742): FATAL EXCEPTION: main
06-30 13:17:07.080: E/AndroidRuntime(1742): java.lang.RuntimeException: Unable to start activity ComponentInfo{thomas.schneider/thomas.schneider.Highscore}: java.lang.NullPointerException
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.os.Looper.loop(Looper.java:137)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-30 13:17:07.080: E/AndroidRuntime(1742): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 13:17:07.080: E/AndroidRuntime(1742): at java.lang.reflect.Method.invoke(Method.java:511)
06-30 13:17:07.080: E/AndroidRuntime(1742): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-30 13:17:07.080: E/AndroidRuntime(1742): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-30 13:17:07.080: E/AndroidRuntime(1742): at dalvik.system.NativeStart.main(Native Method)
06-30 13:17:07.080: E/AndroidRuntime(1742): Caused by: java.lang.NullPointerException
06-30 13:17:07.080: E/AndroidRuntime(1742): at thomas.schneider.Highscore.onCreate(Highscore.java:41)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.app.Activity.performCreate(Activity.java:5008)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-30 13:17:07.080: E/AndroidRuntime(1742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
Kann jemand helfen?