我有一个sqliteDatabase数据成员,我在onCreate初始化并在onPause(),onStop()和onDestroy()上调用.close().它在onResume()中重新初始化.它看起来运行得很好但是当我查看调试器时它看到了:
08-24 20:23:50.014: ERROR/Database(6767): Leak found08-24 20:23:50.014: ERROR/Database(6767): java.lang.IllegalStateException: /data/data/com.hh.Timepunch/databases/times_database.db sqliteDatabase created and never closed08-24 20:23:50.014: ERROR/Database(6767): at androID.database.sqlite.sqliteDatabase.<init>(sqliteDatabase.java:1695)08-24 20:23:50.014: ERROR/Database(6767): at androID.database.sqlite.sqliteDatabase.openDatabase(sqliteDatabase.java:739)08-24 20:23:50.014: ERROR/Database(6767): at androID.database.sqlite.sqliteDatabase.openorCreateDatabase(sqliteDatabase.java:761)08-24 20:23:50.014: ERROR/Database(6767): at androID.database.sqlite.sqliteDatabase.openorCreateDatabase(sqliteDatabase.java:754)08-24 20:23:50.014: ERROR/Database(6767): at androID.app.ApplicationContext.openorCreateDatabase(ApplicationContext.java:476)08-24 20:23:50.014: ERROR/Database(6767): at androID.content.Contextwrapper.openorCreateDatabase(Contextwrapper.java:193)08-24 20:23:50.014: ERROR/Database(6767): at com.hh.Timepunch.TimeSheetActivity.onCreate(TimeSheetActivity.java:72)08-24 20:23:50.014: ERROR/Database(6767): at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)08-24 20:23:50.014: ERROR/Database(6767): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2459)08-24 20:23:50.014: ERROR/Database(6767): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)08-24 20:23:50.014: ERROR/Database(6767): at androID.app.ActivityThread.access00(ActivityThread.java:119)08-24 20:23:50.014: ERROR/Database(6767): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)08-24 20:23:50.014: ERROR/Database(6767): at androID.os.Handler.dispatchMessage(Handler.java:99)08-24 20:23:50.014: ERROR/Database(6767): at androID.os.Looper.loop(Looper.java:123)08-24 20:23:50.014: ERROR/Database(6767): at androID.app.ActivityThread.main(ActivityThread.java:4363)08-24 20:23:50.014: ERROR/Database(6767): at java.lang.reflect.Method.invokeNative(Native Method)08-24 20:23:50.014: ERROR/Database(6767): at java.lang.reflect.Method.invoke(Method.java:521)08-24 20:23:50.014: ERROR/Database(6767): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)08-24 20:23:50.014: ERROR/Database(6767): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:618)08-24 20:23:50.014: ERROR/Database(6767): at dalvik.system.NativeStart.main(Native Method)08-24 20:23:50.054: ERROR/Database(6767): Leak found08-24 20:23:50.054: ERROR/Database(6767): java.lang.IllegalStateException: /data/data/com.hh.Timepunch/databases/times_database.db sqliteDatabase created and never closed08-24 20:23:50.054: ERROR/Database(6767): at androID.database.sqlite.sqliteDatabase.<init>(sqliteDatabase.java:1695)08-24 20:23:50.054: ERROR/Database(6767): at androID.database.sqlite.sqliteDatabase.openDatabase(sqliteDatabase.java:739)08-24 20:23:50.054: ERROR/Database(6767): at androID.database.sqlite.sqliteDatabase.openorCreateDatabase(sqliteDatabase.java:761)08-24 20:23:50.054: ERROR/Database(6767): at androID.database.sqlite.sqliteDatabase.openorCreateDatabase(sqliteDatabase.java:754)08-24 20:23:50.054: ERROR/Database(6767): at androID.app.ApplicationContext.openorCreateDatabase(ApplicationContext.java:476)08-24 20:23:50.054: ERROR/Database(6767): at androID.content.Contextwrapper.openorCreateDatabase(Contextwrapper.java:193)08-24 20:23:50.054: ERROR/Database(6767): at com.hh.Timepunch.TimepunchActivity.onCreate(TimepunchActivity.java:60)08-24 20:23:50.054: ERROR/Database(6767): at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)08-24 20:23:50.054: ERROR/Database(6767): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2459)08-24 20:23:50.054: ERROR/Database(6767): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)08-24 20:23:50.054: ERROR/Database(6767): at androID.app.ActivityThread.access00(ActivityThread.java:119)08-24 20:23:50.054: ERROR/Database(6767): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)08-24 20:23:50.054: ERROR/Database(6767): at androID.os.Handler.dispatchMessage(Handler.java:99)08-24 20:23:50.054: ERROR/Database(6767): at androID.os.Looper.loop(Looper.java:123)08-24 20:23:50.054: ERROR/Database(6767): at androID.app.ActivityThread.main(ActivityThread.java:4363)08-24 20:23:50.054: ERROR/Database(6767): at java.lang.reflect.Method.invokeNative(Native Method)08-24 20:23:50.054: ERROR/Database(6767): at java.lang.reflect.Method.invoke(Method.java:521)08-24 20:23:50.054: ERROR/Database(6767): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)08-24 20:23:50.054: ERROR/Database(6767): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:618)08-24 20:23:50.054: ERROR/Database(6767): at dalvik.system.NativeStart.main(Native Method)08-24 20:23:50.199: ERROR/libs3c2drender(1912): int S3c2DRender::DoG2D(unsigned int, s3c_img*, s3c_rect*, unsigned int, s3c_img*, s3c_rect*, int, int)::S3C_G2D_rotator_18176 fail
我得到这个错误的地方之一就是离开这个活动:
public class TimepunchActivity extends Activity { Calendar timeNow = Calendar.getInstance(); sqliteDatabase timesDatabase; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.timepunch); //Create/open and configure database (if necessary) timesDatabase = openorCreateDatabase( "times_database.db", sqliteDatabase.CREATE_IF_NECESSARY, null); timesDatabase.setLocale(Locale.getDefault()); timesDatabase.setLockingEnabled(false); timesDatabase.setVersion(3); //lenIEnt is set to true so incrementing minute 59 auto increments hour, etc timeNow.setLenIEnt(true); //Create table (if necessary) final String dbCommand = "CREATE table IF NOT EXISTS Timepunches (punch INTEGER PRIMARY KEY);"; timesDatabase.execsql(dbCommand); updateLastInPunch(); button punchbutton = (button)findVIEwByID(R.ID.punch_button); punchbutton.setHapticFeedbackEnabled(true); punchbutton.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { //Insert data ContentValues punchValue = new ContentValues(); punchValue.put("punch", timeNow.getTimeInMillis()); long newPunchID = timesDatabase.insert("Timepunches", null, punchValue); punchValue.clear(); //if punch already exists if (newPunchID < 0) { AlertDialog.Builder builder = new AlertDialog.Builder(TimepunchActivity.this); builder.setTitle("Whoops") .setMessage("I've already recorded a punch for this time and day.") .setCancelable(true) .setNeutralbutton("Oh", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int ID) { dialog.cancel(); } }) .show(); } else { } } //end setonClick() }); //end click Listener for punchbutton } //end onCreate() @OverrIDe public voID onResume() { super.onResume(); checkFirstTimer(); //Open and configure database timesDatabase = openorCreateDatabase( "times_database.db", sqliteDatabase.CREATE_IF_NECESSARY, null); timesDatabase.setLocale(Locale.getDefault()); timesDatabase.setLockingEnabled(false); timesDatabase.setVersion(3); //Update date object for current time timeNow.setTimeInMillis(System.currentTimeMillis()); timeNow.set(Calendar.SECOND, 0); timeNow.set(Calendar.MILliSECOND, 0); updateClock((TextVIEw)findVIEwByID(R.ID.current_time_textBox), timeNow); updateLastInPunch(); } //end onResume() @OverrIDe public voID onPause() { super.onPause(); timesDatabase.close(); } //end onResume() @OverrIDe public voID onStop() { super.onStop(); timesDatabase.close(); } //end onResume() @OverrIDe public voID onDestroy() { super.onDestroy(); timesDatabase.close(); } //end onResume() //Shows last in-punch that has no out-punch // if there is none it hIDes the section public voID updateLastInPunch() { Cursor punchCursor = timesDatabase.query("Timepunches", null, null, null, null, null, "punch ASC;"); TextVIEw inPunchLabel = (TextVIEw)findVIEwByID(R.ID.in_punch_label_textvIEw); TextVIEw inPunchTV = (TextVIEw)findVIEwByID(R.ID.in_punch_time_textvIEw); //odd count means there is a missing punch (assumed to be latest in punch) // therefore show last in punch if ((punchCursor.getCount()%2) != 0) { punchCursor.movetoLast(); Calendar lastIn = Calendar.getInstance(); lastIn.setTimeInMillis(punchCursor.getLong(0)); updateClock((TextVIEw)findVIEwByID(R.ID.in_punch_time_textvIEw), lastIn); inPunchLabel.setVisibility(VIEw.VISIBLE); inPunchLabel.setAnimation(fadeInAnimation); inPunchLabel.startAnimation(fadeInAnimation); inPunchTV.setVisibility(VIEw.VISIBLE); inPunchTV.setAnimation(fadeInAnimation); inPunchTV.startAnimation(fadeInAnimation); } else { //only fade when "turning off" last punch display // (don't fade away when returning to screen) if (inPunchLabel.getVisibility() == VIEw.VISIBLE) { inPunchLabel.setAnimation(fadeOutAnimation); inPunchLabel.startAnimation(fadeOutAnimation); inPunchLabel.setVisibility(VIEw.INVISIBLE); inPunchTV.setAnimation(fadeOutAnimation); inPunchTV.startAnimation(fadeOutAnimation); inPunchTV.setVisibility(VIEw.INVISIBLE); } } punchCursor.close(); } //end getLastInPunch() } //end TimepunchActivity
但奇怪的是,这个错误并不是持久性的.
我是否需要做更多,然后调用.close()?
解决方法:
问题是你在onCreate和onResume中创建/打开它,所以你要做两次.同样,你关闭它两次,所以它可能会崩溃.
一般来说 – 为什么不使用sqliteOpenHelper?这将使管理数据库变得更加容易.
总结以上是内存溢出为你收集整理的java – Android认为我没有关闭我的数据库!为什么?全部内容,希望文章能够帮你解决java – Android认为我没有关闭我的数据库!为什么?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)