我在logcat中得到了这个:
11-11 17:11:42.161: E/AndroidRuntime(23599): FATAL EXCEPTION: main
11-11 17:11:42.161: E/AndroidRuntime(23599): java.lang.NullPointerException
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.example.lightalarmclock.AddBox.setAlarm(AddBox.java:164)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.example.lightalarmclock.AddBox.onOptionsItemSelected(AddBox.java:147)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.app.Activity.onMenuItemSelected(Activity.java:2642)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1039)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.view.View.performClick(View.java:4247)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.view.View$PerformClick.run(View.java:17728)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.os.Handler.handleCallback(Handler.java:730)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.os.Looper.loop(Looper.java:137)
11-11 17:11:42.161: E/AndroidRuntime(23599): at android.app.ActivityThread.main(ActivityThread.java:5298)
11-11 17:11:42.161: E/AndroidRuntime(23599): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 17:11:42.161: E/AndroidRuntime(23599): at java.lang.reflect.Method.invoke(Method.java:525)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
11-11 17:11:42.161: E/AndroidRuntime(23599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
11-11 17:11:42.161: E/AndroidRuntime(23599): at dalvik.system.NativeStart.main(Native Method)
所以错误在第164行出现问题,对吧?
所以这是代码:
private void setAlarm (SQLiteDatabase db){
if (rep1 >= 0 || rep2 >= 0 || rep3 >= 0 || rep4 >= 0 || rep5 >= 0 || rep6 >= 0 || rep7 >= 0){
repB = 1;
} else { repB = 2; }
timeSet= pos4+pos3+":"+pos2+pos1;
db.execSQL("INSERT INTO TABLE alarmStack(alarm_time, alarm_repeat, rep1d, rep2d, rep3d, rep4d, rep5d, rep6d, rep7d) " +
"VALUES (timeSet, repB, rep1, rep2, rep3, rep4, rep5, rep6, rep7)");
}
第164行是db.execSQL。
我想我错误地估算了价值但是我似乎无法修复它所以我来到这里。 我想alarmStack(...)中的值被视为变量,但它们应该是字符串的一部分,以指示我在SQL中编辑的列,但我无法找到解决方案或正确的语法。
编辑:
所以在评论中@ user1676075说“db”var为null,修复我这样做:
private static class dbHelper extends SQLiteOpenHelper{
public dbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//writing to database
if (rep1 >= 0 || rep2 >= 0 || rep3 >= 0 || rep4 >= 0 || rep5 >= 0 || rep6 >= 0 || rep7 >= 0){
repB = 1;
} else { repB = 0; }
//setting time
timeSet= pos4+pos3+":"+pos2+pos1;
//Writing
db.execSQL
("INSERT INTO TABLE alarmDB.alarmStack(alarm_time, alarm_repeat, rep1, rep2, rep3, rep4, rep5, rep6, rep7) " +
"VALUES (timeSet, repB, rep1, rep2, rep3, rep4, rep5, rep6, rep7)");
//launch backToMain
//backToMain(ourCtx);
}
但是现在由于某种原因它无法实例化dbHelper并且我得到了这个:
11-12 20:47:42.852: E/AndroidRuntime(18656): FATAL EXCEPTION: main
11-12 20:47:42.852: E/AndroidRuntime(18656): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lightalarmclock/com.example.lightalarmclock.AddBox$dbHelper}: java.lang.InstantiationException: can't instantiate class com.example.lightalarmclock.AddBox$dbHelper; no empty constructor
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2400)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.access$600(ActivityThread.java:153)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.os.Looper.loop(Looper.java:137)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.main(ActivityThread.java:5298)
11-12 20:47:42.852: E/AndroidRuntime(18656): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 20:47:42.852: E/AndroidRuntime(18656): at java.lang.reflect.Method.invoke(Method.java:525)
11-12 20:47:42.852: E/AndroidRuntime(18656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
11-12 20:47:42.852: E/AndroidRuntime(18656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
11-12 20:47:42.852: E/AndroidRuntime(18656): at dalvik.system.NativeStart.main(Native Method)
11-12 20:47:42.852: E/AndroidRuntime(18656): Caused by: java.lang.InstantiationException: can't instantiate class com.example.lightalarmclock.AddBox$dbHelper; no empty constructor
11-12 20:47:42.852: E/AndroidRuntime(18656): at java.lang.Class.newInstanceImpl(Native Method)
11-12 20:47:42.852: E/AndroidRuntime(18656): at java.lang.Class.newInstance(Class.java:1130)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
11-12 20:47:42.852: E/AndroidRuntime(18656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
11-12 20:47:42.852: E/AndroidRuntime(18656): ... 11 more
答案 0 :(得分:0)
我有类似的问题一次,我不得不添加一个带超级调用的默认构造函数来修复它。你可以尝试一次。
public dbHelper() {
super();
}