在我的主要活动上:
button = (Button) findViewById(R.id.mainAddWatchButton);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(audiovideo, CountyBrowser.class));
}
});
CountyBrowser活动开始很好(并且表现得很好,就像它应该的那样),但是这些东西在LogCat中喷出:
10-11 19:10:41.182: INFO/ActivityManager(58): Starting activity: Intent { cmp=se.ribit.bb/.CountyBrowser }
10-11 19:10:41.782: DEBUG/dalvikvm(368): GC freed 6313 objects / 354184 bytes in 114ms
10-11 19:10:41.812: INFO/dalvikvm(368): Uncaught exception thrown by finalizer (will be discarded):
10-11 19:10:41.812: INFO/dalvikvm(368): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@43d3ec38 on null that has not been deactivated or closed
10-11 19:10:41.823: INFO/dalvikvm(368): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
10-11 19:10:41.823: INFO/dalvikvm(368): at dalvik.system.NativeStart.run(Native Method)
10-11 19:10:41.852: INFO/dalvikvm(368): Uncaught exception thrown by finalizer (will be discarded):
10-11 19:10:41.862: INFO/dalvikvm(368): Ljava/lang/IllegalStateException;: **Finalizing cursor android.database.sqlite.SQLiteCursor@43d3dc80 on null that has not been deactivated or closed**
10-11 19:10:41.862: INFO/dalvikvm(368): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
10-11 19:10:41.873: INFO/dalvikvm(368): at dalvik.system.NativeStart.run(Native Method)
10-11 19:10:42.452: INFO/ActivityManager(58): Displayed activity se.ribit.bb/.CountyBrowser: 1198 ms (total 1198 ms)
10-11 19:10:47.702: DEBUG/dalvikvm(206): GC freed 124 objects / 5424 bytes in 154ms
10-11 19:10:52.692: DEBUG/dalvikvm(105): GC freed 2292 objects / 133288 bytes in 107ms
我正在使用数据库和游标,但上面的消息包含零信息我是否做错了。除了这些日志之外,活动正常。如果我在调试模式下运行所有东西,模拟器会一直把我扔回Eclipse。
我在这里遗漏了什么吗?任何人都在关心我做错了什么?
答案 0 :(得分:1)
我认为你必须在Activity暂停之前关闭游标。在打开新Activity的Activity的onPause方法中,尝试关闭游标。
void onPause(){
myCursor1.close();
myCursor2.close();
}
答案 1 :(得分:0)
经过大量的步骤调试后,我找到了错误的来源。我有这个方法:
private boolean doesColumnExist(String table, int id) {
SQLiteDatabase db = this.dbHelper.getReadableDatabase();
String query = String.format("SELECT count(*) FROM %s WHERE %s = %s",
table, C_ID, id);
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
return (c.getInt(0) == 1);
}
我还不擅长Java,所以天真地认为当这个方法返回时,光标应该自行消失,但我显然在这个事实上非常错误。我不得不将方法重写为:
private boolean doesColumnExist(String table, int id) {
SQLiteDatabase db = this.dbHelper.getReadableDatabase();
String query = String.format("SELECT count(*) FROM %s WHERE %s = %s",
table, C_ID, id);
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
boolean retVal = (c.getInt(0) == 1);
c.close();
return retVal;
}
现在所有的LogCat填充物都消失了。
我从未意识到我必须手动执行c.close()。由于我是Java的新手,我认为Java的GC会处理它。在我的Java书中进一步阅读证明我错了,非常错误。