私は自分の Android アプリを作成しました。データベースを使用します。ここでは onCreate メソッドを使用します。
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_PARK_DETAILS);
insertParks(database);
database.execSQL(CREATE_DIARY);
}
私のSamsung Galaxy S2では、ストアからアプリをダウンロードした他の100人の電話と同様に機能します. しかし、あるユーザーは Android 2.3.4 を搭載した Sony-Ericsson Xperia Arc (LT15i) で問題を抱えています。そのため、別の電話を使用している他の人にアプリをテストするように与えました.これらの1つは、Android 4.1で同じ問題を抱えています.SQLException.
その後、このデバイスでは2番目のテーブルが作成されておらず、最初のテーブルのみが作成されていることがわかりました。
なんで?
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));
LogCat:
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;
}