我希望有人能在这里帮助我。我似乎无法追查造成这种情况的原因。我正在测试Firebase消息。 onMessageReceived成功命中,但是当我调用NotificationManager.notify时出现此错误:
E / UncaughtException:java.lang.RuntimeException:无法销毁 activity {com.myproject.android/com.myproject.android.MainActivity}: java.lang.NullPointerException:observer
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public void onMessageReceived(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
createNotification(notification);
}
}
private void createNotification(RemoteMessage.Notification notification) {
Context context = getBaseContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setContentText(notification.getBody());
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
答案 0 :(得分:0)
我想我明白了。我的MainActivity onDestroy中有一些代码不应该存在。错误让我失望,因为我不知道为什么在这里调用我的MainActivity onDestroy。