[英]Need help with Database Access in Android
我在我的應用程序中使用sqlite數據庫。 我在這樣的服務類中有這個dbhelper類。
public class MushroomService {
private int downloadprogress;
private int databasesize;
private DataBaseHelper myDbHelper;
}
public MushroomService(Context context)
{
myDbHelper = new DataBaseHelper(context);
downloadprogress = 0;
}
服務類是我的應用程序類的成員,如下所示:
public class fungifieldguideapplication extends Application {
public MushroomService service = new MushroomService(this);
}
在我的活動類中,我訪問此應用程序並將其保留為本地變量,如下所示:
public class Cat_Genus extends Activity {
fungifieldguideapplication appState;
ListView list_Genus;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cat_genus);
appState = ((fungifieldguideapplication)this.getApplication());
Cursor cursor_genuslist = appState.service.GetGenusList(this);
startManagingCursor(cursor_genuslist);
}
}
我的服務電話如下所示:
public Cursor GetGenusList(Context context)
{
myDbHelper = new DataBaseHelper(context);
Cursor cursor;
try
{
myDbHelper.openDataBase();
SQLiteDatabase db = myDbHelper.myDataBase;
cursor = db.query(true, "Mushrooms", GenusListColumns, null, null, "Genus", null, "Genus ASC", null);
}
catch(SQLException sqle)
{
throw sqle;
}
return cursor;
}
我添加了這些替代,以消除我的內存泄漏。
@Override
public void onDestroy() {
list_Genus.setAdapter(null);
appState.service.CloseDB();
super.onDestroy();
}
@Override
public void onPause() {
appState.service.CloseDB();
super.onPause();
}
@Override
public void onResume() {
appState.service.OpenDB();
super.onResume();
}
但是我無法阻止我的內存泄漏。 有人可以幫我弄清楚如何擺脫這些內存泄漏嗎? 這些泄漏屬於以下類型:
07-27 17:38:44.613: ERROR/Database(26175): Leak found
07-27 17:38:44.613: ERROR/Database(26175): java.lang.IllegalStateException: /data/data/net.daleroy.fungifieldguide/databases/Mushrooms.db SQLiteDatabase created and never closed
07-27 17:38:44.613: ERROR/Database(26175): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)
07-27 17:38:44.613: ERROR/Database(26175): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
07-27 17:38:44.613: ERROR/Database(26175): at net.daleroy.fungifieldguide.data.DataBaseHelper.openDataBase(DataBaseHelper.java:79)
07-27 17:38:44.613: ERROR/Database(26175): at net.daleroy.fungifieldguide.services.MushroomService.GetSpeciesListByGenus(MushroomService.java:113)
07-27 17:38:44.613: ERROR/Database(26175): at net.daleroy.fungifieldguide.activities.Cat_Species.onCreate(Cat_Species.java:46)
07-27 17:38:44.613: ERROR/Database(26175): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 17:38:44.613: ERROR/Database(26175): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-27 17:38:44.613: ERROR/Database(26175): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-27 17:38:44.613: ERROR/Database(26175): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-27 17:38:44.613: ERROR/Database(26175): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-27 17:38:44.613: ERROR/Database(26175): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 17:38:44.613: ERROR/Database(26175): at android.os.Looper.loop(Looper.java:123)
07-27 17:38:44.613: ERROR/Database(26175): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-27 17:38:44.613: ERROR/Database(26175): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 17:38:44.613: ERROR/Database(26175): at java.lang.reflect.Method.invoke(Method.java:521)
07-27 17:38:44.613: ERROR/Database(26175): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-27 17:38:44.613: ERROR/Database(26175): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-27 17:38:44.613: ERROR/Database(26175): at dalvik.system.NativeStart.main(Native Method)
也許我是錯的,但也許是因為您在與數據庫進行交互之后沒有關閉數據庫。 每當我在android上進行數據庫工作時,我都會在Try {} finally {}子句中包圍交互,該子句會在finally上關閉連接。
例如
try{
//database stuff
} finally{
db.close
}
您的程序正在終止,因為您兩次打開數據庫
myDbHelper = new DataBaseHelper(context); // first here
Cursor cursor;
try
{
myDbHelper.openDataBase(); //then here
您可能可以刪除“ myDbHelper.openDataBase();”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.