我要构建一个闹钟应用程序,它将在设置的时间(而不是振动)写入Firebase数据库。 我使用https://github.com/PPartisan/Simple-Alarms代码作为闹钟的基本代码,但遇到错误。如果某人具有类似应用程序或链接的工作代码将很有帮助。
public final class AlarmReceiver extends BroadcastReceiver {
private Firebase fire;
Context thiscontext;
private static final String BUNDLE_EXTRA = "bundle_extra";
private static final String ALARM_KEY = "alarm_key";
@Override
public void onReceive(Context context, Intent intent) {
Firebase.setAndroidContext(AlarmReceiver.this.thiscontext);
final Alarm alarm = intent.getBundleExtra(BUNDLE_EXTRA).getParcelable(ALARM_KEY);
assert alarm != null;
final int id = AlarmUtils.getNotificationId(alarm);
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Intent notifIntent = new Intent(context, AlarmLandingPageActivity.class);
notifIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PendingIntent pIntent = PendingIntent.getActivity(
context, id, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"M_CH_ID");
builder.setSmallIcon(R.drawable.ic_alarm_white_24dp);
builder.setColor(ContextCompat.getColor(context, R.color.accent));
builder.setContentTitle(context.getString(R.string.app_name));
builder.setContentText(alarm.getLabel());
builder.setTicker(alarm.getLabel());
builder.setVibrate(new long[] {1000,500,1000,500,1000,500});
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setContentIntent(pIntent);
builder.setAutoCancel(true);
builder.setPriority(Notification.PRIORITY_HIGH);
fire = new Firebase("https://alarmclock-8u7i2.firebaseio.com/");
Firebase fireChild = fire.child("Alarm");
fireChild.setValue("ALARM ON");
manager.notify(id, builder.build());
//Reset Alarm manually
setReminderAlarm(context, alarm);
}
错误:Process: com.banaao.makersplace.progx, PID: 5561
java.lang.RuntimeException: Unable to start receiver com.banaao.makersplace.progx.service.AlarmReceiver: java.lang.NullPointerException: Can't pass null for argument 'context' in setAndroidContext()
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Can't pass null for argument 'context' in setAndroidContext()
at com.firebase.client.Firebase.setAndroidContext(Firebase.java:972)
at com.banaao.makersplace.progx.service.AlarmReceiver.onReceive(AlarmReceiver.java:37)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)