请注意,我正在设计一个应用程序,该应用程序涉及在第一次运行时创建数据库,并在运行中打开它以跟随。所以我在“onCreate()”方法的“MainActiviy”类中以以下方式处理它:
try
{
myDataBase=this.openOrCreateDatabase("wishes", MODE_PRIVATE, null);
myDataBase.execSQL("CREATE TABLE IF NOT EXISTS " + tablename
+
" (_id VARCHAR(15) primary key,Name VARCHAR(20), Message VARCHAR(100),Event VARCHAR(15),Date INT);");
} //SQLiteDatabase 'myDatabase' is declared as public
现在我有另一个名为“eventdefinition”的类,其中我创建了“MainActivity”对象“ma”,然后我尝试在按钮单击事件的表中插入值,如下所示:
MainActivity ma=new MainActivity(); //line 63
event=spinner.getSelectedItem().toString(); //line 64
message=ed1.getText().toString(); //line 65
ma.myDataBase.openDatabase("/data/data/org.mithil.autowish/databases/wishes", null,ma.myDataBase.OPEN_READWRITE); //line 66
ma.myDataBase.execSQL("INSERT INTO " +
ma.tablename +
" Values ('"+ finalnumber +"','"+ name +"','"+ message +"','"+ event +"','"+ ldate +"');");
代码给我 NullPointerException 如下:
05-10 11:03:30.992: E/AndroidRuntime(602): FATAL EXCEPTION: main
05-10 11:03:30.992: E/AndroidRuntime(602): java.lang.NullPointerException
05-10 11:03:30.992: E/AndroidRuntime(602): at org.mithil.autowish.eventdefinition$2.onClick(eventdefinition.java:67)
05-10 11:03:30.992: E/AndroidRuntime(602): at android.view.View.performClick(View.java:3480)
05-10 11:03:30.992: E/AndroidRuntime(602): at android.view.View$PerformClick.run(View.java:13983)
05-10 11:03:30.992: E/AndroidRuntime(602): at android.os.Handler.handleCallback(Handler.java:605)
05-10 11:03:30.992: E/AndroidRuntime(602): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 11:03:30.992: E/AndroidRuntime(602): at android.os.Looper.loop(Looper.java:137)
05-10 11:03:30.992: E/AndroidRuntime(602): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-10 11:03:30.992: E/AndroidRuntime(602): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 11:03:30.992: E/AndroidRuntime(602): at java.lang.reflect.Method.invoke(Method.java:511)
05-10 11:03:30.992: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-10 11:03:30.992: E/AndroidRuntime(602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-10 11:03:30.992: E/AndroidRuntime(602): at dalvik.system.NativeStart.main(Native Method)
请让我知道我在哪里做错了。任何帮助都将意味着很多。在此先感谢。
问候