Problem with a notification: null object reference

0

I am trying to make a notification from a class without activity, I have this code, which gives the following error I have tested verifying if the notification was null but not null, I do not know what the house can be

   private Intent getNotificationIntent() {
        Intent intent = new Intent(this, MapsActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        return intent;
    }

    private void showActionButtonsNotification() {
        Intent yesIntent = getNotificationIntent();
        yesIntent.setAction(LAUNCH);

        Intent maybeIntent = getNotificationIntent();
        maybeIntent.setAction(START);

        Intent noIntent = getNotificationIntent();
        noIntent.setAction(STOP);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, getNotificationIntent(), PendingIntent.FLAG_UPDATE_CURRENT))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("Action Buttons Notification Received")
                .setContentTitle("Route: ")
                .setContentText("This is even more text.")
                .setWhen(System.currentTimeMillis())
                .setOngoing(true)
                .setAutoCancel(true)
                .addAction(new NotificationCompat.Action(
                        R.mipmap.ic_launcher,
                        "Launch map",
                        PendingIntent.getActivity(getApplicationContext(), 0, yesIntent, PendingIntent.FLAG_UPDATE_CURRENT)))
                .addAction(new NotificationCompat.Action(
                        R.mipmap.ic_launcher,
                        "Start",
                        PendingIntent.getActivity(getApplicationContext(), 0, maybeIntent, PendingIntent.FLAG_UPDATE_CURRENT)))
                .addAction(new NotificationCompat.Action(
                        R.mipmap.ic_launcher,
                        "Stop",
                        PendingIntent.getActivity(getApplicationContext(), 0, noIntent, PendingIntent.FLAG_UPDATE_CURRENT)))
                .build();

        Log.e("a","--------"+notification.toString());
        if(notification.equals(null)){
            Log.e("a","_________________________________awdw_--");
        }else{
            notificationManager.notify(NOTIFY_ID, notification);
        }
    }

  /*  @Override
    protected void onNewIntent(Intent intent) {
        processIntentAction(intent);

    }

    private void processIntentAction(Intent intent) {
        if (intent.getAction() != null) {
            switch (intent.getAction()) {
                case LAUNCH:
                    Toast.makeText(this, "Launching ", Toast.LENGTH_SHORT).show();
                    break;
                case START:
                    Toast.makeText(this, "Starting ", Toast.LENGTH_SHORT).show();
                    break;
                case STOP:
                    Toast.makeText(this, "Stopping ", Toast.LENGTH_SHORT).show();
                    break;
            }
        }*/

this is the error it gives:

java.lang.RuntimeException: Unable to create service ticnor.es.map2.ServiceUN: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.NotificationManager.notify(int, android.app.Notification)' on a null object reference
                                                 at android.app.ActivityThread.handleCreateService(ActivityThread.java:2771)
                                                 at android.app.ActivityThread.access$1800(ActivityThread.java:151)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:135)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.NotificationManager.notify(int, android.app.Notification)' on a null object reference
                                                 at ticnor.es.map2.ServiceUN.showActionButtonsNotification(ServiceUN.java:278)
                                                 at ticnor.es.map2.ServiceUN.onCreate(ServiceUN.java:154)
                                                 at android.app.ActivityThread.handleCreateService(ActivityThread.java:2761)
    
asked by Aurelio Fernandez 18.04.2018 в 10:14
source

1 answer

0

What is null is not the notification itself, but the notificationManager variable. Check the point where you are initializing that variable.

    
answered by 18.04.2018 / 11:16
source