【问题标题】:Need help with Database Access in Android需要有关 Android 中的数据库访问的帮助
【发布时间】:2011-03-21 20:47:02
【问题描述】:

我在我的应用程序中使用了一个 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)

【问题讨论】:

    标签: java android database sqlite memory-leaks


    【解决方案1】:

    也许我错了,但也许是因为您在与数据库交互后没有关闭它。每当我在 android 上进行 db 工作时,我都会在 Try { } finally { } 子句中围绕交互,该子句在 finally 上关闭连接。

    例如

    try{
    //database stuff
    } finally{
       db.close
     }
    

    【讨论】:

      【解决方案2】:

      您的程序正在终止,因为您打开了两次数据库

          myDbHelper = new DataBaseHelper(context); // first here
          Cursor cursor;
          try 
          {
              myDbHelper.openDataBase(); //then here
      

      您可能可以删除 'myDbHelper.openDataBase();'

      【讨论】:

      • 我的程序没有终止,它只是导致内存泄漏。它也不是每次都这样做。
      • 好的,所以你的程序没有终止,但你是否测试过'myDbHelper.openDataBase();'造成内存泄漏?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      相关资源
      最近更新 更多