当我启动一个活动时,我一直收到错误,起初它是空指针异常但现在我不知道错误是什么,它只是不再工作
logcat的:
02-18 13:15:28.885: D/dalvikvm(328): GC_EXTERNAL_ALLOC freed 50K, 53% free 2553K/5379K, external 2730K/3266K, paused 115ms
02-18 13:15:29.762: D/dalvikvm(328): GC_EXTERNAL_ALLOC freed 1K, 53% free 2552K/5379K, external 6764K/8447K, paused 51ms
02-18 13:15:45.312: D/dalvikvm(328): GC_EXTERNAL_ALLOC freed 11K, 52% free 2583K/5379K, external 8695K/10350K, paused 46ms
02-18 13:15:45.582: D/AndroidRuntime(328): Shutting down VM
02-18 13:15:45.582: W/dalvikvm(328): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-18 13:15:45.592: E/AndroidRuntime(328): FATAL EXCEPTION: main
02-18 13:15:45.592: E/AndroidRuntime(328): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.connectfour/com.example.connectfour.ScoreActivity}: java.lang.NullPointerException
02-18 13:15:45.592: E/AndroidRuntime(328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 13:15:45.592: E/AndroidRuntime(328): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 13:15:45.592: E/AndroidRuntime(328): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 13:15:45.592: E/AndroidRuntime(328): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 13:15:45.592: E/AndroidRuntime(328): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 13:15:45.592: E/AndroidRuntime(328): at android.os.Looper.loop(Looper.java:123)
02-18 13:15:45.592: E/AndroidRuntime(328): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 13:15:45.592: E/AndroidRuntime(328): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 13:15:45.592: E/AndroidRuntime(328): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 13:15:45.592: E/AndroidRuntime(328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 13:15:45.592: E/AndroidRuntime(328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 13:15:45.592: E/AndroidRuntime(328): at dalvik.system.NativeStart.main(Native Method)
02-18 13:15:45.592: E/AndroidRuntime(328): Caused by: java.lang.NullPointerException
02-18 13:15:45.592: E/AndroidRuntime(328): at com.example.connectfour.Database.getScores(Database.java:57)
02-18 13:15:45.592: E/AndroidRuntime(328): at com.example.connectfour.ScoreActivity$MyCursorAdapter.<init>(ScoreActivity.java:21)
02-18 13:15:45.592: E/AndroidRuntime(328): at com.example.connectfour.ScoreActivity.onCreate(ScoreActivity.java:49)
02-18 13:15:45.592: E/AndroidRuntime(328): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 13:15:45.592: E/AndroidRuntime(328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 13:15:45.592: E/AndroidRuntime(328): ... 11 more
02-18 13:15:54.303: I/Process(328): Sending signal. PID: 328 SIG: 9
代码
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
}
}
代码分数活动:这是分数活动第21行,其中超级东西是:
@SuppressWarnings("deprecation")
public MyCursorAdapter(Context context) {
super(context, new Database(context).getScores());
scoreIdx = getCursor().getColumnIndex(Database.KEY_NAME);
// TODO Auto-generated constructor stub
}
答案 0 :(得分:0)
创建数据库类时,仍然没有实现myDatabase变量。
所以,当你在做getScores时,你将会有一个NPE。
之前尝试打开()。
答案 1 :(得分:0)
您尚未初始化ourDatabase
。首先通过调用open()
...
public MyCursorAdapter(Context context) {
super(context, new Database(context).open().getScores(););
scoreIdx = getCursor().getColumnIndex(Database.KEY_NAME);
// TODO Auto-generated constructor stub
}
答案 2 :(得分:0)
试试这个。
public Database open() {
ourHelper = new DBHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public Cursor getScores() {
ourDatabase = open();
return ourDatabase.rawQuery("SELECT * FROM "+DATABASE_TABLE, null); //No Parameter
close() ;
}
这里,当您调用getScores时,将创建db并将其分配给ourDatabase,然后可以轻松使用。如果有效,请告诉我。