好的,所以我的应用程序在 Android 2.3 上完美运行,在 4.0.4 上崩溃。
我正在解析 XML 并将数据保存到我的 SQLite 数据库。
这不是每次都会发生,但确实经常发生,有时在按钮点击时有时在新的 Activity 启动时......等等
崩溃异常:
11-16 15:16:41.231: E/SQLiteQuery(6398): exception: library routine called out of sequence; query: SELECT _id, name, tipus, azonosito, gpsx, gpsy, pictures, telefonok, webcim, email, nyitvatartas, cim, orszag, befogadokepesseg, szolgaltatasok, arkategoria, leiras, ertekeles, pontszam, facebook, keywords, rating FROM shops_top20
11-16 15:16:41.241: E/AndroidRuntime(6398): FATAL EXCEPTION: main
11-16 15:16:41.241: E/AndroidRuntime(6398): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.KeySoft.OpenGuide/com.KeySoft.OpenGuide.Activities.Top20}: android.database.sqlite.SQLiteMisuseException: library routine called out of sequence
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.os.Looper.loop(Looper.java:137)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.app.ActivityThread.main(ActivityThread.java:4507)
11-16 15:16:41.241: E/AndroidRuntime(6398): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 15:16:41.241: E/AndroidRuntime(6398): at java.lang.reflect.Method.invoke(Method.java:511)
11-16 15:16:41.241: E/AndroidRuntime(6398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
11-16 15:16:41.241: E/AndroidRuntime(6398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
11-16 15:16:41.241: E/AndroidRuntime(6398): at dalvik.system.NativeStart.main(Native Method)
11-16 15:16:41.241: E/AndroidRuntime(6398): Caused by: android.database.sqlite.SQLiteMisuseException: library routine called out of sequence
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.database.sqlite.SQLiteQuery.nativeFillWindow(Native Method)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:86)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:201)
11-16 15:16:41.241: E/AndroidRuntime(6398): at com.KeySoft.OpenGuide.Activities.Top20.SqlShopsVissza(Top20.java:546)
11-16 15:16:41.241: E/AndroidRuntime(6398): at com.KeySoft.OpenGuide.Activities.Top20.listakKirajzolasaEsValtas(Top20.java:165)
11-16 15:16:41.241: E/AndroidRuntime(6398): at com.KeySoft.OpenGuide.Activities.Top20.onCreate(Top20.java:113)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.app.Activity.performCreate(Activity.java:4469)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
11-16 15:16:41.241: E/AndroidRuntime(6398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
编辑:好的,所以我在关闭应用程序中的所有数据库连接之前关闭了所有游标,现在我得到了这个例外:
11-16 20:00:28.409: E/AndroidRuntime(7548): FATAL EXCEPTION: main
11-16 20:00:28.409: E/AndroidRuntime(7548): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.KeySoft.OpenGuide/com.KeySoft.OpenGuide.Activities.Top20}: java.util.ConcurrentModificationException
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.os.Looper.loop(Looper.java:137)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.app.ActivityThread.main(ActivityThread.java:4507)
11-16 20:00:28.409: E/AndroidRuntime(7548): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 20:00:28.409: E/AndroidRuntime(7548): at java.lang.reflect.Method.invoke(Method.java:511)
11-16 20:00:28.409: E/AndroidRuntime(7548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
11-16 20:00:28.409: E/AndroidRuntime(7548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
11-16 20:00:28.409: E/AndroidRuntime(7548): at dalvik.system.NativeStart.main(Native Method)
11-16 20:00:28.409: E/AndroidRuntime(7548): Caused by: java.util.ConcurrentModificationException
11-16 20:00:28.409: E/AndroidRuntime(7548): at java.util.WeakHashMap$HashIterator.next(WeakHashMap.java:165)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1250)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.database.sqlite.SQLiteDatabase.close(SQLiteDatabase.java:1220)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.database.sqlite.SQLiteOpenHelper.close(SQLiteOpenHelper.java:265)
11-16 20:00:28.409: E/AndroidRuntime(7548): at com.KeySoft.OpenGuide.DB_Adapter.close(DB_Adapter.java:273)
11-16 20:00:28.409: E/AndroidRuntime(7548): at com.KeySoft.OpenGuide.Activities.Top20.SqlShopsVissza(Top20.java:558)
11-16 20:00:28.409: E/AndroidRuntime(7548): at com.KeySoft.OpenGuide.Activities.Top20.listakKirajzolasaEsValtas(Top20.java:165)
11-16 20:00:28.409: E/AndroidRuntime(7548): at com.KeySoft.OpenGuide.Activities.Top20.onCreate(Top20.java:113)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.app.Activity.performCreate(Activity.java:4469)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
11-16 20:00:28.409: E/AndroidRuntime(7548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
我的代码中有问题的部分是我的 Top20 Activity 中的一个函数,目前是以下函数:
protected void getShopsFromSql(String whichTable){
db.open();
Cursor c;
if(whichTable.equals("Top20")){
c = db.getAllTop20Titles();
}
else if (whichTable.equals("Latest10")){
c = db.getAllLatest10Titles();
}
else{
c = db.getAllShopsFullTitles();
}
if (c.moveToFirst()) {
do {
addShopToList(c.getString(1), c.getString(2), c.getString(4), c.getString(5), c.getString(12), c.getString(11), c.getString(3));
}while (c.moveToNext());
}
c.close();
db.close(); //App Crashes here... why?
}
db 肯定是我的数据库,我用谷歌搜索了这个错误,当然,当我想在一个线程上编辑一个值并通过另一个线程(这次是后台 AsyncTask)迭代相同的值时会发生这种情况,但我有一个单例数据库适配器我在堆栈中读到这种数据库实现最适合 Android 应用程序。
我有一个带有单个数据库的扩展应用程序类,我每次都调用这个数据库。
public class MyApp extends Application {
private static DB_Adapter globalDatabase;
@Override
public void onCreate() {
super.onCreate();
globalDatabase = new DB_Adapter(this);
}
public static DB_Adapter getDB() {
return globalDatabase;
}
}
在我的应用程序中引用数据库,如下所示:
public class Top20 extends Activity
{
private DB_Adapter db;
private MyApp myApp;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.top20);
myApp = (MyApp) getApplication();
db = myApp.getDB();
//...........morecode...etc..
所以:
我的问题是:
为什么会出现这个错误?我做对了这个单例数据库实现吗?
谢谢你的建议,亚当