我尝试db_insert.java
从MyDB.java
.
public void append(String n,int p) {
String str = "INSERT INTO " + TABLE_NAME + " (" + NAME + "," + PRICE + ") VALUES (" + n +"," + p + ")";
db.execSQL(str);
}
在调试模式下,n
&p
从db_insert.java
.
到目前为止,还好。但是当处理时db.execSQL(str);
,应用程序会崩溃。
有人知道为什么吗?
日志猫
02-07 00:16:48.467: I/Process(979): Sending signal. PID: 979 SIG: 9 02-07 00:17:17.827:
E/MyDBlog(1109): click insert_OKs 02-07 00:17:17.827: D/AndroidRuntime(1109): Shutting
down VM 02-07 00:17:17.827: W/dalvikvm(1109): threadid=1: thread exiting with uncaught
exception (group=0x2aac8578) 02-07 00:17:17.827: E/AndroidRuntime(1109): FATAL EXCEPTION:
main 02-07 00:17:17.827: E/AndroidRuntime(1109): java.lang.NullPointerException 02-07
00:17:17.827: E/AndroidRuntime(1109): at com.example.mydb.MyDB.append(MyDB.java:39) 02-07
00:17:17.827: E/AndroidRuntime(1109): at com.example.mydb.db_insert$1.onClick(db_insert.java:66) 02-07 00:17:17.827:
E/AndroidRuntime(1109): at android.view.View.performClick(View.java:2552) 02-07
00:17:17.827: E/AndroidRuntime(1109): at android.view.View$PerformClick.run(View.java:9229)
02-07 00:17:17.827: E/AndroidRuntime(1109): at android.os.Handler.handleCallback(Handler.java:587) 02-07 00:17:17.827:
E/AndroidRuntime(1109): at android.os.Handler.dispatchMessage(Handler.java:92) 02-07
00:17:17.827: E/AndroidRuntime(1109): at android.os.Looper.loop(Looper.java:130) 02-07
00:17:17.827: E/AndroidRuntime(1109): at android.app.ActivityThread.main(ActivityThread.java:3701) 02-07 00:17:17.827: E/AndroidRuntime(1109): at java.lang.reflect.Method.invokeNative(Native Method) 02-07 00:17:17.827: E/AndroidRuntime(1109): at java.lang.reflect.Method.invoke(Method.java:507) 02-07 00:17:17.827: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 02-07
00:17:17.827: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624) 02-07 00:17:17.827:
E/AndroidRuntime(1109): at dalvik.system.NativeStart.main(Native Method)
我的应用首先使用 open() 打开数据库。
MyDB.java
public SQLiteDatabase db=null;
private Context mCtx = null;
public MyDB(Context ctx){
this.mCtx = ctx;
}
public void open() throws SQLException { // 開啟已經存在的資料庫
db = mCtx.openOrCreateDatabase(DATABASE_NAME, 0, null);
try {
db.execSQL(CREATE_TABLE);// 建立資料表
}catch (Exception e) {
}
}