在我修改数据库以添加 4 个新列(studentN、teacherN、studentBD 和teacherBD)之前,我的应用程序运行良好。添加后,当我调用 on pause 方法时,我的应用程序崩溃:
protected void onPause() {
super.onPause();
SaveVariableStatus();
List<StatParcours> StatParcours = db.getAllSats();
}
问题似乎与 getAllStats() 叠加,所以这里是代码:hole databasehandler:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION =10;
// Database Name
private static final String DATABASE_NAME = "StatParcours";
// Stats table name
private static final String TABLE_STATS = "Statistique";
// Stats Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_DATE = "date";
private static final String KEY_SPEED = "speed";
private static final String KEY_DIST = "distance";
private static final String KEY_COND = "condition";
private static final String KEY_DURA = "duration";
private static final String KEY_STUDN = "studentN";
private static final String KEY_TEACHN = "teacherN";
private static final String KEY_STUDBD = "studentBD";
private static final String KEY_TEACHBD = "teacherBD";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_STATS_TABLE = "CREATE TABLE " + TABLE_STATS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_DATE + " TEXT ," + KEY_DURA + " TEXT,"
+ KEY_SPEED + " DOUBLE," + KEY_DIST + " DOUBLE,"
+ KEY_COND + " TEXT," + KEY_STUDN + " TEXT,"
+ KEY_TEACHN + " TEXT," + KEY_STUDBD + " TEXT,"
+ KEY_TEACHBD + " TEXT" + ")";
db.execSQL(CREATE_STATS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATS);
// Create tables again
onCreate(db);
}
public List<StatParcours> getAllSats() {
List<StatParcours> statList = new ArrayList<StatParcours>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_STATS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
StatParcours stat = new StatParcours();
stat.setID(Integer.parseInt(cursor.getString(0)));
stat.setDate(cursor.getString(1));
stat.setDuration(cursor.getString(2));
stat.setDistance(Double.parseDouble(cursor.getString(3)));
stat.setSpeed(Double.parseDouble(cursor.getString(4)));
stat.setCondition(cursor.getString(5));
stat.setStudentName(cursor.getString(6));
stat.setStudentBirthDate(cursor.getString(7));
stat.setTeacherName(cursor.getString(8));
stat.setTeacherBirthDate(cursor.getString(9));
// Adding contact to list
statList.add(stat);
} while (cursor.moveToNext());
}
// return contact list
return statList;
}
}
日志猫:
07-24 10:20:48.834: E/AndroidRuntime(18746): FATAL EXCEPTION: main
07-24 10:20:48.834: E/AndroidRuntime(18746): java.lang.RuntimeException: Unable to pause activity {com.intellibatteries.conduiteaccompagnee/com.intellibatteries.conduiteaccompagnee.Maps}: java.lang.NullPointerException
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2879)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2835)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2813)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.ActivityThread.access$800(ActivityThread.java:140)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.os.Looper.loop(Looper.java:137)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-24 10:20:48.834: E/AndroidRuntime(18746): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 10:20:48.834: E/AndroidRuntime(18746): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 10:20:48.834: E/AndroidRuntime(18746): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-24 10:20:48.834: E/AndroidRuntime(18746): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-24 10:20:48.834: E/AndroidRuntime(18746): at dalvik.system.NativeStart.main(Native Method)
07-24 10:20:48.834: E/AndroidRuntime(18746): Caused by: java.lang.NullPointerException
07-24 10:20:48.834: E/AndroidRuntime(18746): at java.lang.StringToReal.parseDouble(StringToReal.java:244)
07-24 10:20:48.834: E/AndroidRuntime(18746): at java.lang.Double.parseDouble(Double.java:295)
07-24 10:20:48.834: E/AndroidRuntime(18746): at com.intellibatteries.conduiteaccompagnee.DatabaseHandler.getAllSats(DatabaseHandler.java:133)
07-24 10:20:48.834: E/AndroidRuntime(18746): at com.intellibatteries.conduiteaccompagnee.Maps.onPause(Maps.java:396)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.Activity.performPause(Activity.java:5304)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1229)
07-24 10:20:48.834: E/AndroidRuntime(18746): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2866)
07-24 10:20:48.834: E/AndroidRuntime(18746): ... 12 more