-2

我在我的 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 是空的,为了解决这个问题,我这样做了:

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
4

1 回答 1

0

我曾经遇到过类似的问题,我不得不添加一个带有超级调用的默认构造函数来修复它。你可以尝试一次。

     public dbHelper() {
       super();
     }
于 2013-11-12T20:11:12.750 回答