我正在开发一个应用程序来存储两个人之间的一些赌注。
现在我对此非常陌生,尤其是使用 SQLite。问题是,尽管我的数据库中只有 2 或 3 个条目,但我一直收到以下错误消息之一。
1号
09-22 10:19:36.138: E/AndroidRuntime(21351): FATAL EXCEPTION: main
09-22 10:19:36.138: E/AndroidRuntime(21351): java.lang.OutOfMemoryError
09-22 10:19:36.138: E/AndroidRuntime(21351): at java.util.Vector.newElementArray(Vector.java:120)
09-22 10:19:36.138: E/AndroidRuntime(21351): at java.util.Vector.growByOne(Vector.java:466)
09-22 10:19:36.138: E/AndroidRuntime(21351): at java.util.Vector.add(Vector.java:153)
09-22 10:19:36.138: E/AndroidRuntime(21351): at challenge.sql.CustomDataSource.getAllEntries(CustomDataSource.java:172)
09-22 10:19:36.138: E/AndroidRuntime(21351): at challenge.main.OverviewActivity.updateOverview(OverviewActivity.java:40)
09-22 10:19:36.138: E/AndroidRuntime(21351): at challenge.main.OverviewActivity.onCreate(OverviewActivity.java:29)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.app.Activity.performCreate(Activity.java:4465)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.os.Looper.loop(Looper.java:137)
09-22 10:19:36.138: E/AndroidRuntime(21351): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-22 10:19:36.138: E/AndroidRuntime(21351): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:19:36.138: E/AndroidRuntime(21351): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:19:36.138: E/AndroidRuntime(21351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-22 10:19:36.138: E/AndroidRuntime(21351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-22 10:19:36.138: E/AndroidRuntime(21351): at dalvik.system.NativeStart.main(Native Method)
2号
09-22 10:19:20.308: E/AndroidRuntime(21337): FATAL EXCEPTION: main
09-22 10:19:20.308: E/AndroidRuntime(21337): java.lang.OutOfMemoryError: [memory exhausted]
09-22 10:19:20.308: E/AndroidRuntime(21337): at dalvik.system.NativeStart.main(Native Method)
09-22 10:19:20.318: E/System(21337): Uncaught exception thrown by finalizer
09-22 10:19:20.378: E/System(21337): java.lang.IllegalStateException: Don't have database lock!
09-22 10:19:20.378: E/System(21337): at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
09-22 10:19:20.378: E/System(21337): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
09-22 10:19:20.378: E/System(21337): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
09-22 10:19:20.378: E/System(21337): at android.util.LruCache.trimToSize(LruCache.java:197)
09-22 10:19:20.378: E/System(21337): at android.util.LruCache.evictAll(LruCache.java:285)
09-22 10:19:20.378: E/System(21337): at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
09-22 10:19:20.378: E/System(21337): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
09-22 10:19:20.378: E/System(21337): at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
09-22 10:19:20.378: E/System(21337): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
09-22 10:19:20.378: E/System(21337): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
09-22 10:19:20.378: E/System(21337): at java.lang.Thread.run(Thread.java:856)
3号
09-22 10:19:20.308: E/SQLiteDatabase(21337): close() was never explicitly called on database '/data/data/challenge.main/databases/DBCHALLENGES'
09-22 10:19:20.308: E/SQLiteDatabase(21337): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at challenge.sql.CustomDataSource.open(CustomDataSource.java:33)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at challenge.main.OverviewActivity.updateOverview(OverviewActivity.java:38)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at challenge.main.OverviewActivity.onCreate(OverviewActivity.java:29)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.app.Activity.performCreate(Activity.java:4465)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.os.Looper.loop(Looper.java:137)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-22 10:19:20.308: E/SQLiteDatabase(21337): at dalvik.system.NativeStart.main(Native Method)
现在问题是 - 正如我认为的那样 - 是由我课堂getAllEntries()
上的这种方法引起的OverviewActivity
public void updateOverview(){
datasource = new CustomDataSource(this);
datasource.open();
Game game = datasource.getSpecificGame(challengeName);
entries = datasource.getAllEntries();
datasource.close();
(...)
}
这是CustomDataSource
类中的代码
public Vector<FootballEntry> getAllEntries(){
Vector<FootballEntry> entries = new Vector<FootballEntry>();
Cursor cursor = database.query(CustomSQLiteHelper.TABLE_FOOTBALL, allColumns2, null, null, null, null, null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
FootballEntry entry = cursorToEntry(cursor);
entries.add(entry);
cursor.moveToNext();
}
cursor.close();
return entries;
}
private FootballEntry cursorToEntry(Cursor cursor){
cursor.moveToFirst();
FootballEntry game = new FootballEntry();
game.setID(cursor.getLong(0));
game.setDate(cursor.getString(1));
game.setHome(cursor.getInt(2));
game.setPlatform(cursor.getString(3));
game.setTeam1(cursor.getString(4));
game.setStars1(cursor.getDouble(5));
game.setTeam2(cursor.getString(6));
game.setStars2(cursor.getDouble(7));
game.setGoals1(cursor.getInt(8));
game.setGoals2(cursor.getInt(9));
game.setFinished(cursor.getInt(10));
return game;
}
这里的构造函数CustomDataSource
和方法open()
和close()
public CustomDataSource(Context context){
dbHelper = new CustomSQLiteHelper(context);
}
public void open() throws SQLException{
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
注意:我读过几次,我的 CustomSQLiteHelper 应该扩展 SQLiteOpenHelper,就是这样。
我不完全明白为什么会抛出这个错误。每次我想使用数据库时,我都会创建一个新的CustomDataSource
、调用open()
、执行工作和调用close()
。这里有什么问题?首先我想,错误是由我的Activity的方法和方法中的puttin引起的open()
,但删除它们不会造成任何差异。同样,每个活动我只创建一个实例,并在使用它之后关闭它。onResume()
close()
onClose()
CustomDataSource