在前面的問題中,我建議我不需要維護數據庫,因爲設備因使用本地數據庫而旋轉。旋轉設備時發生崩潰 - 生命週期問題
我認爲我至少可以維護我的列表的數據源。我在嘗試執行此操作時遇到了問題,因爲我收到錯誤,說沒有在數據庫上調用close()。儘管我在onDestroy中調用close。
任何人都可以看到我在哪裏錯了嗎?我只想通過維護數據以在旋轉設備後重新填充列表來改善用戶體驗。
是困惑我的另一件事是我的日誌報表中按以下順序出來:
- 的onCreate航班
- 第一設置
- 重複使用的數據源
- 破壞活性
我希望在重新使用數據源之前銷燬活動發生。
package com.testing.flights;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.widget.SimpleCursorAdapter;
public class FlightListActivity extends ListActivity {
private SQLiteDatabase database;
private String fields[] = {BaseColumns._ID, "name", "flights", "distance"};
private SimpleCursorAdapter datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(ACTIVITY_SERVICE, "onCreate flights");
datasource = (SimpleCursorAdapter) getLastNonConfigurationInstance();
if (datasource == null) {
database = getData();
datasource = getCursor();
Log.v(ACTIVITY_SERVICE, "first setup");
}
setListAdapter(datasource);
}
protected SQLiteDatabase getData() {
DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
return myDbHelper.openDataBase();
}
protected CustomCursorAdapter getCursor() {
Cursor data = database.query("pilots", fields, null, null, null, null, null);
final CustomCursorAdapter mysource = new CustomCursorAdapter(this, R.layout.row, data, fields, new int[] { R.id.id, R.id.name, R.id.flights, R.id.distance });
return mysource;
}
@Override
public Object onRetainNonConfigurationInstance() {
Log.v(ACTIVITY_SERVICE, "reusing datasource");
final SimpleCursorAdapter myData = datasource;
return myData;
}
@Override
protected void onDestroy() {
Log.v(ACTIVITY_SERVICE, "destroying activity");
database.close();
super.onDestroy();
}
}
LogCat:
04-03 16:22:55.900: E/SQLiteDatabase(1860): close() was never explicitly called on database '/data/data/com.testing.flights/databases/club'
當你有任何錯誤總是發表您的logcat – 2012-04-03 15:34:48
@Samir - 我已經添加了錯誤 – Neil 2012-04-03 15:36:47
尼爾,看看官方Android開發者博客,有一個整體上張貼。 – 2012-04-03 15:42:49