我已经编写了我的 android 应用程序,它使用了一个数据库,这里是 onCreate 方法:
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_PARK_DETAILS);
insertParks(database);
database.execSQL(CREATE_DIARY);
}
在我的三星 Galaxy S2 上它可以工作,就像其他 100 人从商店下载应用程序的手机一样。但是,一位用户在使用 Android 2.3.4 的 Sony-Ericsson Xperia Arc (LT15i) 上遇到问题。所以我给使用不同手机的其他人测试该应用程序,其中一个在 Android 4.1 上存在相同的问题......一个 SQLException。
之后我发现在这个设备上没有创建第二个表,而只创建了第一个表。
为什么?
CREATE_PARK_DETAILS:
CREATE TABLE cableparks (id int, name varchar(100), city varchar(100), state varchar(50), logo varchar(50), xCoord int, yCoord int, url varchar(100) DEFAULT '', street varchar(100) DEFAULT '', cap varchar(10) DEFAULT '', PRIMARY KEY (id));
创建日记:
CREATE TABLE diary (id int, comment text, addDate long, PRIMARY KEY (id), FOREIGN KEY (id) REFERENCES cableparks (id));
日志猫:
08-21 21:29:01.402: E/SQLiteLog(12388): (1) no such table: diary
08-21 21:29:01.402: D/dalvikvm(12388): GC_CONCURRENT freed 176K, 7% free
10130K/10823K, paused 4ms+7ms, total 61ms
08-21 21:29:01.414: W/CursorWrapperInner(12388): Cursor finalized without prior close()
08-21 21:29:01.453: E/TAG(12388): getFavorite
08-21 21:29:01.453: E/TAG(12388): android.database.sqlite.SQLiteException: no such table: diary (code 1): , while compiling: SELECT * FROM diary WHERE id=31;
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
08-21 21:29:01.453: E/TAG(12388): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
08-21 21:29:01.453: E/TAG(12388): at com.emaborsa.cablePark.db.DatabaseManager.getFavorite(DatabaseManager.java:133)
08-21 21:29:01.453: E/TAG(12388): at com.emaborsa.cablePark.db.DatabaseManager.getPark(DatabaseManager.java:122)
08-21 21:29:01.453: E/TAG(12388): at com.emaborsa.cablePark.activities.SinglePark.onCreate(SinglePark.java:120)
08-21 21:29:01.453: E/TAG(12388): at android.app.Activity.performCreate(Activity.java:5008)
08-21 21:29:01.453: E/TAG(12388): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-21 21:29:01.453: E/TAG(12388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-21 21:29:01.453: E/TAG(12388): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-21 21:29:01.453: E/TAG(12388): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-21 21:29:01.453: E/TAG(12388): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-21 21:29:01.453: E/TAG(12388): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 21:29:01.453: E/TAG(12388): at android.os.Looper.loop(Looper.java:137)
08-21 21:29:01.453: E/TAG(12388): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-21 21:29:01.453: E/TAG(12388): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 21:29:01.453: E/TAG(12388): at java.lang.reflect.Method.invoke(Method.java:511)
08-21 21:29:01.453: E/TAG(12388): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 21:29:01.453: E/TAG(12388): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 21:29:01.453: E/TAG(12388): at dalvik.system.NativeStart.main(Native Method)
08-21 21:29:01.496: D/AndroidRuntime(12388): Shutting down VM
08-21 21:29:01.496: W/dalvikvm(12388): threadid=1: thread exiting with uncaught exception (group=0x41b35300)
08-21 21:29:01.504: E/AndroidRuntime(12388): FATAL EXCEPTION: main
08-21 21:29:01.504: E/AndroidRuntime(12388): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emaborsa.cablePark/com.emaborsa.cablePark.activities.SinglePark}: java.lang.NullPointerException
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.os.Looper.loop(Looper.java:137)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-21 21:29:01.504: E/AndroidRuntime(12388): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 21:29:01.504: E/AndroidRuntime(12388): at java.lang.reflect.Method.invoke(Method.java:511)
08-21 21:29:01.504: E/AndroidRuntime(12388): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 21:29:01.504: E/AndroidRuntime(12388): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 21:29:01.504: E/AndroidRuntime(12388): at dalvik.system.NativeStart.main(Native Method)
08-21 21:29:01.504: E/AndroidRuntime(12388): Caused by: java.lang.NullPointerException
08-21 21:29:01.504: E/AndroidRuntime(12388): at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java:312)
08-21 21:29:01.504: E/AndroidRuntime(12388): at com.emaborsa.cablePark.map.route.LocationOverlay.addOverlay(LocationOverlay.java:21)
08-21 21:29:01.504: E/AndroidRuntime(12388): at com.emaborsa.cablePark.activities.SinglePark.setViewValues(SinglePark.java:180)
08-21 21:29:01.504: E/AndroidRuntime(12388): at com.emaborsa.cablePark.activities.SinglePark.onCreate(SinglePark.java:129)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.app.Activity.performCreate(Activity.java:5008)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-21 21:29:01.504: E/AndroidRuntime(12388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-21 21:29:01.504: E/AndroidRuntime(12388): ... 11 more
GetFavorite 方法:
public FavoritePark getFavorite(int parkId) {
FavoritePark favorite = null;
String query = "SELECT * FROM " + TABLE_DIARY + " WHERE " + ID + "=" + parkId + ";";
try {
Cursor cursor = database.rawQuery(query, null);
try {
if (cursor.moveToFirst()) {
int commentIndex = cursor.getColumnIndex(COMMENT);
String comment = cursor.getString(commentIndex);
int addedIndex = cursor.getColumnIndex(ADD_TIME);
Date added = new Date(cursor.getLong(addedIndex));
favorite = new FavoritePark(comment, added);
}
} finally {
try { cursor.close(); } catch (Throwable ignore) {}
}
} catch (Exception e) {
Log.e("TAG", "getFavorite", e);
}
return favorite;
}