简体   繁体   English

尝试在空对象引用上调用虚拟方法“android.app.Notification androidx.core.app.NotificationCompat$Builder.build()”

[英]Attempt to invoke virtual method 'android.app.Notification androidx.core.app.NotificationCompat$Builder.build()' on a null object reference

I am using Firebase Push notification to send notification to app.我正在使用 Firebase 推送通知向应用发送通知。 Notification is working in Latest android version's but when i tried to test in Lolipop version .When i test the notification app crashed .通知在最新的 android 版本中工作,但是当我尝试在 Lolipop 版本中进行测试时。当我测试通知应用程序崩溃时。 I don't know where is the problem.我不知道问题出在哪里。 Please help me to solve the issue.请帮我解决这个问题。 Thanks in advance .提前致谢 。

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());


    }


}

Logcat:日志猫:

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)

Try this method:试试这个方法:

 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());
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 应用程序因“尝试在 null object 参考上调用虚拟方法”而崩溃 - Android app crashes with “Attempt to invoke virtual method on a null object reference” 如何修复在 null object 参考上调用虚拟方法 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' 的尝试? - How to fix attempt to invoke virtual method 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' on a null object reference? 应用程序崩溃:尝试对空对象引用调用虚拟方法 - App crashed : Attempt to invoke virtual method on a null object reference 由于尝试在 null object 引用上调用虚拟方法“”,应用程序崩溃 - App crashes because of attempt to invoke virtual method ' ' on a null object reference java.lang.NullPointerException:尝试在 null ZA8CFDE6331BD54B66AC96F8911 参考上调用虚拟方法 'void androidx.appcompat.app.ActionBar.hide()' - java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.hide()' on a null object reference 使用SQLite数据的Android应用“试图在空对象引用上调用虚拟方法'...'” - Android app using SQLite data “Attempt to invoke virtual method '…' on a null object reference” 尝试在空对象引用上调用虚拟方法“android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()” - Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference 尝试在空对象引用上调用虚方法'void android.app.ActionBar.setHomeButtonEnabled(boolean)' - Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference 尝试在空对象引用 Android 适配器上调用虚拟方法 - Attempt to invoke virtual method on a null object reference Android Adapter 尝试对空对象引用调用虚拟方法“” - Attempt to invoke virtual method ' 'on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM