在设备上崩溃

时间:2011-12-21 07:02:13

标签: android database crash

我有一个应用程序,我使用数据库在列表视图上插入值。当我在合并后在模拟器上运行应用程序它运行正常但在设备插入值时崩溃,LogCat向我显示:关闭光标但我已经关闭了所有开始打开的coursers。 这个按钮动作

plus.setOnClickListener(new View.OnClickListener() {


    public void onClick(View v) 
    {

        tx = txt.getText().toString();
        postive.add(tx);
        if(tx.length() >0)
        {
            myDB.execSQL("INSERT INTO UP (ID,UPNAME) VALUES ('"+idnum+"','"+tx+"')");

            txt.setText("");            
            function();
             results.clear();
            Intent i=new  Intent(Positive.this,NewgridActivity.class);
            bundle.putInt("tabid", 1);
            bundle.putInt("id", idnum);
            i.putExtras(bundle);
            startActivity(i);
            finish();
        }
        else
            DisplayToast("TextField Empty !!!!");

    }
});

请帮助。

logcat中的

错误

12-21 13:11:02.582: E/Database(4909): close() was never explicitly called on database '/data/data/pa.gd/databases/DECISION_GRID' 
12-21 13:11:02.582: E/Database(4909): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
12-21 13:11:02.582: E/Database(4909):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
12-21 13:11:02.582: E/Database(4909):   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
12-21 13:11:02.582: E/Database(4909):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
12-21 13:11:02.582: E/Database(4909):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
12-21 13:11:02.582: E/Database(4909):   at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
12-21 13:11:02.582: E/Database(4909):   at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-21 13:11:02.582: E/Database(4909):   at pa.gd.PAIDGRIDActivity.onCreate(PAIDGRIDActivity.java:66)
12-21 13:11:02.582: E/Database(4909):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-21 13:11:02.582: E/Database(4909):   at dalvik.system.NativeStart.main(Native Method)

关闭数据库时记录错误

我已关闭数据库,然后我尝试添加一个值,它会因此日志错误而崩溃

12-21 15:45:58.043: E/AndroidRuntime(401): FATAL EXCEPTION: main
12-21 15:45:58.043: E/AndroidRuntime(401): java.lang.IllegalStateException: database not open
12-21 15:45:58.043: E/AndroidRuntime(401):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1723)
12-21 15:45:58.043: E/AndroidRuntime(401):  at dc.grd.Positive$1.onClick(Positive.java:77)
12-21 15:45:58.043: E/AndroidRuntime(401):  at android.view.View.performClick(View.java:2408)
12-21 15:45:58.043: E/AndroidRuntime(401):  at android.view.View$PerformClick.run(View.java:8816)
12-21 15:45:58.043: E/AndroidRuntime(401):  at android.os.Handler.handleCallback(Handler.java:587)
12-21 15:45:58.043: E/AndroidRuntime(401):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 15:45:58.043: E/AndroidRuntime(401):  at android.os.Looper.loop(Looper.java:123)
12-21 15:45:58.043: E/AndroidRuntime(401):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-21 15:45:58.043: E/AndroidRuntime(401):  at java.lang.reflect.Method.invokeNative(Native Method)
12-21 15:45:58.043: E/AndroidRuntime(401):  at java.lang.reflect.Method.invoke(Method.java:521)
12-21 15:45:58.043: E/AndroidRuntime(401):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-21 15:45:58.043: E/AndroidRuntime(401):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-21 15:45:58.043: E/AndroidRuntime(401):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

考虑数据库连接的生命周期。我建议您在应用程序类或活动的onResume()方法中打开数据库。关闭onPause()中的数据库。

每当您使用数据库时,请确保在完成所有游标操作后正确关闭它们。

顺便说一下:每次你写一个像你一样的问题标题,一只小猫就会死...

答案 1 :(得分:0)

  

您似乎正在将myDB用于数据库对象。在这种情况下,使用它来关闭它:myDB.close();. - Sander van't Veer   &LT;

  1. myDB.close(); Sander Vant Veer提出的解决方案