我正在使用Android数据库并且已经设置好了;但当我在onClickListener中调用它时,应用程序崩溃了。
我正在使用的代码是
db.open();
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
s = WorkoutChoice.this.weight.getText().toString();
s2 = WorkoutChoice.this.height.getText().toString();
int w = Integer.parseInt(s);
double h = Double.parseDouble(s2);
double BMI = (w/h)/h;
t.setText(""+BMI);
long id = db.insertTitle("001", ""+days, ""+BMI);
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
}
});
db.close();
和我运行它时的日志cat是:
04-01 18:21:54.704: E/global(6333): Deprecated Thread methods are not supported.
04-01 18:21:54.704: E/global(6333): java.lang.UnsupportedOperationException
04-01 18:21:54.704: E/global(6333): at java.lang.VMThread.stop(VMThread.java:85)
04-01 18:21:54.704: E/global(6333): at java.lang.Thread.stop(Thread.java:1391)
04-01 18:21:54.704: E/global(6333): at java.lang.Thread.stop(Thread.java:1356)
04-01 18:21:54.704: E/global(6333): at com.b00348312.workout.Splashscreen$1.run(Splashscreen.java:42)
04-01 18:22:09.444: D/dalvikvm(6333): GC_FOR_MALLOC freed 4221 objects / 252640 bytes in 31ms
04-01 18:22:09.474: I/dalvikvm(6333): Total arena pages for JIT: 11
04-01 18:22:09.574: D/dalvikvm(6333): GC_FOR_MALLOC freed 1304 objects / 302920 bytes in 29ms
04-01 18:22:09.744: D/dalvikvm(6333): GC_FOR_MALLOC freed 2480 objects / 290848 bytes in 33ms
04-01 18:22:10.034: D/dalvikvm(6333): GC_FOR_MALLOC freed 6334 objects / 374152 bytes in 36ms
04-01 18:22:14.344: D/AndroidRuntime(6333): Shutting down VM
04-01 18:22:14.344: W/dalvikvm(6333): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
04-01 18:22:14.364: E/AndroidRuntime(6333): FATAL EXCEPTION: main
04-01 18:22:14.364: E/AndroidRuntime(6333): java.lang.IllegalStateException: database not open
04-01 18:22:14.364: E/AndroidRuntime(6333): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1567)
04-01 18:22:14.364: E/AndroidRuntime(6333): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1484)
04-01 18:22:14.364: E/AndroidRuntime(6333): at com.b00348312.workout.DataBaseHelper.insertTitle(DataBaseHelper.java:84)
04-01 18:22:14.364: E/AndroidRuntime(6333): at com.b00348312.workout.WorkoutChoice$3.onClick(WorkoutChoice.java:84)
04-01 18:22:14.364: E/AndroidRuntime(6333): at android.view.View.performClick(View.java:2408)
04-01 18:22:14.364: E/AndroidRuntime(6333): at android.view.View$PerformClick.run(View.java:8817)
04-01 18:22:14.364: E/AndroidRuntime(6333): at android.os.Handler.handleCallback(Handler.java:587)
04-01 18:22:14.364: E/AndroidRuntime(6333): at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 18:22:14.364: E/AndroidRuntime(6333): at android.os.Looper.loop(Looper.java:144)
04-01 18:22:14.364: E/AndroidRuntime(6333): at android.app.ActivityThread.main(ActivityThread.java:4937)
04-01 18:22:14.364: E/AndroidRuntime(6333): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 18:22:14.364: E/AndroidRuntime(6333): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 18:22:14.364: E/AndroidRuntime(6333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-01 18:22:14.364: E/AndroidRuntime(6333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-01 18:22:14.364: E/AndroidRuntime(6333): at dalvik.system.NativeStart.main(Native Method)
我注意到应用程序打开时出现错误,但我不知道它们来自哪里。
当我拿出与数据库有关的陈述时,没有错误,并且顺利运行。
答案 0 :(得分:1)
我认为你没有打开和关闭数据库。在使用Cursor之前
db.open();
使用光标后
db.close();
尝试这件事。
答案 1 :(得分:1)
如果数据库仅在您设置侦听器时打开,则无效,在使用时必须打开它。在open()
。{/ p>中调用close()
和onCLick()
答案 2 :(得分:0)
你需要在android中使用sqlite的最佳实践。
Sqlite无法正确处理多个连接(线程),即使它们已正确同步。使用帮助程序或内容提供程序。