Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试调用虚拟方法';android.app.Notification androidx.core.app.NotificationCompat$Builder.build()';关于空对象引用_Java_Android_Firebase Cloud Messaging_Android 5.0 Lollipop - Fatal编程技术网

Java 尝试调用虚拟方法';android.app.Notification androidx.core.app.NotificationCompat$Builder.build()';关于空对象引用

Java 尝试调用虚拟方法';android.app.Notification androidx.core.app.NotificationCompat$Builder.build()';关于空对象引用,java,android,firebase-cloud-messaging,android-5.0-lollipop,Java,Android,Firebase Cloud Messaging,Android 5.0 Lollipop,我正在使用Firebase推送通知向应用程序发送通知。通知在最新的android版本中运行,但当我尝试在Lolipop版本中测试时,通知应用程序崩溃。我不知道问题出在哪里。请帮我解决这个问题。提前谢谢 public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(@NonNull RemoteMessage re

我正在使用Firebase推送通知向应用程序发送通知。通知在最新的android版本中运行,但当我尝试在Lolipop版本中测试时,通知应用程序崩溃。我不知道问题出在哪里。请帮我解决这个问题。提前谢谢

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        String title = Objects.requireNonNull(remoteMessage.getNotification()).getTitle();
        String message = remoteMessage.getNotification().getBody();
           //33
        sendNotification(title,message);
    }

    private void sendNotification(String messageTitle,String messageBody) {
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant")
            NotificationChannel notificationChannel=new NotificationChannel("my_notification","n_channel",NotificationManager.IMPORTANCE_MAX);
            notificationChannel.setDescription("description");
            notificationChannel.setName("Channel Name");
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);
        }



        NotificationCompat.Builder notificationBuilder = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.sicont)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.sicont))
                    .setContentTitle(messageTitle)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationManager.IMPORTANCE_MAX)
                    .setOnlyAlertOnce(true)
                    .setChannelId("my_notification")
                    .setColor(Color.parseColor("#3F5996"));
        }
        //.setProgress(100,50,false);
        assert notificationManager != null;
        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);

        assert notificationBuilder != null;
         //76 number line
        notificationManager.notify(m, notificationBuilder.build());


    }


}
日志:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification androidx.core.app.NotificationCompat$Builder.build()' on a null object reference
        at p.MyFirebaseMessagingService.sendNotification(MyFirebaseMessagingService.java:76)
        at p.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:33)
        at com.google.firebase.messaging.FirebaseMessagingService.zzc(com.google.firebase:firebase-messaging@@20.0.0:68)
        at com.google.firebase.messaging.zze.run(com.google.firebase:firebase-messaging@@20.0.0:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source)
尝试以下方法:

 private void sendNotification(String messageTitle,String messageBody) {
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant")
            NotificationChannel notificationChannel=new NotificationChannel("my_notification","n_channel",NotificationManager.IMPORTANCE_MAX);
            notificationChannel.setDescription("description");
            notificationChannel.setName("Channel Name");
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);
        }


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.sicont)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.sicont))
                    .setContentTitle(messageTitle)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setOnlyAlertOnce(true)
                    .setChannelId("my_notification")
                    .setColor(Color.parseColor("#3F5996"));

        //.setProgress(100,50,false);
        assert notificationManager != null;
        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(m, notificationBuilder.build());
    }

if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u CODES.N)
–您只在牛轧糖和更高版本上分配了
notificationBuilder
,因此棒棒糖上的值显然为空。我如何解决这个问题?删除
if