Android:在操作系统启动时重新创建警报时出现空指针异常?
我将在Android操作系统启动时创建并重新创建警报 这就是我在清单中使用此代码的原因:Android:在操作系统启动时重新创建警报时出现空指针异常?,android,notifications,android-ndk,system,android-manifest,Android,Notifications,Android Ndk,System,Android Manifest,我将在Android操作系统启动时创建并重新创建警报 这就是我在清单中使用此代码的原因: <receiver android:name=".RecreateTwoMonthAlarm" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <
<receiver android:name=".RecreateTwoMonthAlarm" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
//但我得到了符号错误,比如:MODE\u WORLD\u READABLE无法解析为变量
Calendar calendar_GST_18_June_2011 = Calendar.getInstance();
calendar_GST_18_June_2011.setTimeInMillis(System.currentTimeMillis());
calendar_GST_18_June_2011.set(2011, 5, 18, myPrefs.getInt("hour", 00), myPrefs.getInt("minute", 00), 0); // here i got exception
if(!(calendar_GST_18_June_2011.getTimeInMillis()<=currentTime)){
AM_2M_GST_1 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
intent = new Intent(context, AlarmReceiverNotificationForTwoMonth.class);
intent.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 20th June 2011.");
PI_2M_GST_1 = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AM_2M_GST_1.set(AlarmManager.RTC_WAKEUP, calendar_GST_18_June_2011.getTimeInMillis(), PI_2M_GST_1);
}
// for the GST 19 August 2011
Calendar calendar_GST_17_August_2011 = Calendar.getInstance();
calendar_GST_17_August_2011.setTimeInMillis(System.currentTimeMillis());
calendar_GST_17_August_2011.set(2011, 7, 17,myPrefs.getInt("hour", 00), myPrefs.getInt("minute", 00), 0);
if(!(calendar_GST_17_August_2011.getTimeInMillis()<=currentTime)){
AM_2M_GST_2 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent in2 = new Intent(context, AlarmReceiverNotificationForTwoMonth.class);
in2.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 19th August 2011.");
PI_2M_GST_2 = PendingIntent.getBroadcast(context, 1, in2, PendingIntent.FLAG_UPDATE_CURRENT);
AM_2M_GST_2.set(AlarmManager.RTC_WAKEUP, calendar_GST_17_August_2011.getTimeInMillis(),PI_2M_GST_2);
}
请帮帮我
谢谢。尝试使用Context.WORLD\u MODE\u READABLE,根据调用这段代码的位置,它可能不会认为“this”是Context类型(例如在匿名函数或内部类中)获取NullPointer异常?log在哪里?我已经注释了我得到异常的那一行。不管怎样,让我在第二次到达时把日志放好。等待更新a我正在处理另一个项目。myPrefs在哪里启动?从这个角度来看,我希望它是空的。是的,可能是真的。现在我不打算初始化。若我要在我的代码开始之前初始化它,我会得到syntex错误。@LalitPoptani:请查看更新的问题。
Calendar calendar_GST_18_June_2011 = Calendar.getInstance();
calendar_GST_18_June_2011.setTimeInMillis(System.currentTimeMillis());
calendar_GST_18_June_2011.set(2011, 5, 18, myPrefs.getInt("hour", 00), myPrefs.getInt("minute", 00), 0); // here i got exception
if(!(calendar_GST_18_June_2011.getTimeInMillis()<=currentTime)){
AM_2M_GST_1 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
intent = new Intent(context, AlarmReceiverNotificationForTwoMonth.class);
intent.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 20th June 2011.");
PI_2M_GST_1 = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AM_2M_GST_1.set(AlarmManager.RTC_WAKEUP, calendar_GST_18_June_2011.getTimeInMillis(), PI_2M_GST_1);
}
// for the GST 19 August 2011
Calendar calendar_GST_17_August_2011 = Calendar.getInstance();
calendar_GST_17_August_2011.setTimeInMillis(System.currentTimeMillis());
calendar_GST_17_August_2011.set(2011, 7, 17,myPrefs.getInt("hour", 00), myPrefs.getInt("minute", 00), 0);
if(!(calendar_GST_17_August_2011.getTimeInMillis()<=currentTime)){
AM_2M_GST_2 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent in2 = new Intent(context, AlarmReceiverNotificationForTwoMonth.class);
in2.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 19th August 2011.");
PI_2M_GST_2 = PendingIntent.getBroadcast(context, 1, in2, PendingIntent.FLAG_UPDATE_CURRENT);
AM_2M_GST_2.set(AlarmManager.RTC_WAKEUP, calendar_GST_17_August_2011.getTimeInMillis(),PI_2M_GST_2);
}
02-03 12:19:27.612: ERROR/AndroidRuntime(246): FATAL EXCEPTION: main
02-03 12:19:27.612: ERROR/AndroidRuntime(246): java.lang.RuntimeException: Unable to start receiver com.project.TaxToolbox.RecreateTwoMonthAlarm: java.lang.NullPointerException
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.os.Looper.loop(Looper.java:123)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at java.lang.reflect.Method.invoke(Method.java:521)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at dalvik.system.NativeStart.main(Native Method)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): Caused by: java.lang.NullPointerException
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at com.project.TaxToolbox.RecreateTwoMonthAlarm.onReceive(RecreateTwoMonthAlarm.java:46)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
02-03 12:19:27.612: ERROR/AndroidRuntime(246): ... 10 more