-1

我尝试使用以下代码行从给定行中的名称值的表中删除一行:

db.delete(Table, Column_Name+"="+ col_value, null);

创建表使用:

db.execSQL("create table"+ Table+"("+Column_Name+" text," +Id+" integer primary key)");

Logcat 在删除行的行显示 NPE。这里面有什么问题?

日志猫:

07-31 18:00:13.267: E/AndroidRuntime(1264): java.lang.IllegalStateException: Could not execute method of the activity
07-31 18:00:13.267: E/AndroidRuntime(1264):     at android.view.View$1.onClick(View.java:2072)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at android.view.View.performClick(View.java:2408)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at android.view.View$PerformClick.run(View.java:8816)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at android.os.Handler.handleCallback(Handler.java:587)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at android.os.Looper.loop(Looper.java:123)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at java.lang.reflect.Method.invoke(Method.java:521)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at dalvik.system.NativeStart.main(Native Method)
07-31 18:00:13.267: E/AndroidRuntime(1264): Caused by: java.lang.reflect.InvocationTargetException
07-31 18:00:13.267: E/AndroidRuntime(1264):     at com.example.databaseaccess.MainActivity.deleteone(MainActivity.java:70)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at java.lang.reflect.Method.invoke(Method.java:521)
07-31 18:00:13.267: E/AndroidRuntime(1264):     at android.view.View$1.onClick(View.java:2067)
07-31 18:00:13.267: E/AndroidRuntime(1264):     ... 11 more
07-31 18:00:13.267: E/AndroidRuntime(1264): Caused by: java.lang.NullPointerException
07-31 18:00:13.267: E/AndroidRuntime(1264):     at com.example.databaseaccess.DbHelper.fdelete(DbHelper.java:145)
07-31 18:00:13.267: E/AndroidRuntime(1264):     ... 15 more

第 144 行:字符串 s = Name+"="+"?";

第 145 行:db.delete(Table,s,new String[]{i});

4

2 回答 2

0

该表可能一开始就没有正确创建:

"create table"+ Table+"("

确保“创建表”和名称之间有空格。所以应该是

"create table "+ Table+"("
于 2013-07-31T12:24:06.697 回答
0
db.delete(Table, String.format("%s = ?", Column_Name), new String[] {col_value});
于 2013-07-31T12:25:25.340 回答