Nullpointerexception when displaying Notification with Pendingintent and Broadcastreceiver

Asked

Viewed 15 times

0

I am consuming an API and called Broadcastreceiver whenever the user connects to the internet. However, when displaying the notification is appearing:
"java.lang.Nullpointerexception: Attempt to invoke virtual method 'android.content.Contentresolver android.content.Context.getContentResolver()' on a null Object Reference"

NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID);
    builder.setSmallIcon(R.drawable.ic_tem_pedido);
    builder.setContentTitle("Novo Pedido !");
    builder.setContentText("Favor, verificar o sistema !");
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setDefaults(
            NotificationCompat.DEFAULT_VIBRATE
    );
    Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
    notificationIntent.setData(Uri.parse("https:www.google.com"));

    PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this,
            0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    builder.addAction(R.drawable.ic_visitar_site,"Visitar site",pi);

    NotificationManagerCompat notificationManagerCompat =
            NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());

In this line:

PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this,
            0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  • Are you using this code where? In a separate class or on Main ?

  • The problem is that the context MainActivity.this is void!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.