我的应用程序检查onResume中的日期,以查看是否需要调用MainActivity中名为changeReminder的方法。也就是说,如果日期与存储日期不同,则调用changeReminder(还从其他方法调用changeReminder,例如刷新按钮)。 changeReminder依次调用Reminder类,该类将在创建实例时创建新的提醒。
现在,我正在向应用添加闹钟,广播接收器和通知。因为从MainActivity调用了更改提醒,所以我在BroadcastReceiver类中创建了一个活动实例,但由于提醒类需要上下文(它始终来自MainActivity),因此会抛出错误。在这种情况下,有没有办法从广播接收器成功传递上下文?我想我在下面列出了相关代码,但如果有必要,我可以添加更多代码......
触发警报后记录:
05-02 13:23:00.712 15511-15511/com.mycompany.dudesmyreminders I/System.out﹕ onReceive
05-02 13:23:00.722 15511-15511/com.mycompany.dudesmyreminders I/System.out﹕ start changeReminder
05-02 13:23:00.722 15511-15511/com.mycompany.dudesmyreminders I/System.out﹕ start Reminder
05-02 13:23:00.752 15511-15511/com.mycompany.dudesmyreminders E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mycompany.dudesmyreminders, PID: 15511
java.lang.RuntimeException: Unable to start receiver com.mycompany.dudesmyreminders.ReminderReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2726)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:267)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.mycompany.dudesmyreminders.dbDataSource.open(dbDataSource.java:39)
at com.mycompany.dudesmyreminders.Reminder.<init>(Reminder.java:37)
at com.mycompany.dudesmyreminders.MainActivity.changeReminder(MainActivity.java:283)
at com.mycompany.dudesmyreminders.ReminderReceiver.onReceive(ReminderReceiver.java:23)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2712)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
MainActivity onCreate中的警报管理器:
//Alarm manager for notifications....
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
System.out.println("Hour = " + Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, Calendar.getInstance().get(Calendar.MINUTE) + 2);
System.out.println("Minute = " + Calendar.getInstance().get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(MainActivity.this, ReminderReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
广播接收器:
public class ReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
System.out.println("onReceive");
MainActivity ma = new MainActivity();
ma.changeReminder(context);
String message = ma.getStoredPreference("CurrentMessage");
NewReminderNotification.notify(context,message,1);
}
}
主要活动中的changeReminder:
public void changeReminder(Context context) {
System.out.println("start changeReminder");
Reminder reminder = new Reminder(this);
// Save the message to the Shared Preferences file
int altitude = getStoredPreferenceInt(SEEKBAR_CURRENT_INT);
savePreference("CurrentMessage", reminder.determineReminder(altitude));
savePreferenceInt(DBDATA_ID, (int) reminder.dbData.getId());
//populates the reminder dbData for current session
//DBDATA_ID is only changed in the line above
dbDataCurrentReminder = datasource.getDB_Row_FromID(dbRTable, (long) getStoredPreferenceInt(DBDATA_ID));
//display the message just saved
DisplaySavedMessage();
// save the current date to Preferences
saveDate();
setToggles(0);
}
提醒构造函数:
public Reminder(Context context) {
System.out.println("start Reminder");
ds = new dbDataSource(context);
ds.open();
DatabaseVersion = ds.DatabaseVersion;
}