首页 > 解决方案 > PendingIntent from Notifications are not coming back to service. Showing java.lang.NullPointerException

问题描述

I have created a foreground service hence implemented notifications with PendingIntents.

The Notification is also showing.

But the pause and stop buttons are not working. When I click on it, the app crashes. When I click on play my log is printed means it is working fine.

The error it returns is like:

2021-06-20 23:40:24.167 25285-25285/com.example.startedservice E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.startedservice, PID: 25285
java.lang.RuntimeException: Unable to start service com.example.startedservice.Service.MusicPlayerBoundService@2cdba7d with Intent { flg=0x10000000 cmp=com.example.startedservice/.Service.MusicPlayerBoundService bnds=[382,1495][698,1639] }: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4001)
    at android.app.ActivityThread.access$1800(ActivityThread.java:229)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1896)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:226)
    at android.app.ActivityThread.main(ActivityThread.java:7178)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
    at com.example.startedservice.Service.MusicPlayerBoundService.onStartCommand(MusicPlayerBoundService.java:52)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3968)
    at android.app.ActivityThread.access$1800(ActivityThread.java:229) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1896) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:226) 
    at android.app.ActivityThread.main(ActivityThread.java:7178) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942) 

My onStartCommand() method:

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    String temp =intent.getAction();

    switch(temp){

        case "PLAY": {
            Log.d("MyTag", "play called");
            play();
            break;
        }

        case "PAUSE": {
            Log.d("MyTag", "pause called");
            pause();
            break;
        }

        case "START": {
            createNotificationChannel();
            showNotification();
            break;
        }

        case "STOP": {
            stopForeground(true);
            stopSelf();
            break;
        }

        default:
            stopSelf();


    }

    Log.d("MyTag","onStartCommand Called");
    return START_REDELIVER_INTENT;
}

createNotificationChannel():

private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    "ForegroundServiceChannel",
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

showNotification():

private void showNotification(){

    Notification.Builder builder = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        builder = new Notification.Builder(getApplicationContext(),"ForegroundServiceChannel");
    }

   
    Intent play = new Intent(getApplicationContext(),MusicPlayerBoundService.class);
    play.setAction("PLAY");

    PendingIntent pIntent = PendingIntent.getService(getApplicationContext(),100,play,0);



    Intent pause = new Intent(getApplicationContext(),MusicPlayerBoundService.class);
    play.setAction("PAUSE");

    PendingIntent paIntent = PendingIntent.getService(getApplicationContext(),100,pause,0);


    Intent stop = new Intent(getApplicationContext(),MusicPlayerBoundService.class);
    play.setAction("STOP");

    PendingIntent sIntent = PendingIntent.getService(getApplicationContext(),100,stop,0);



    builder.setSmallIcon(R.mipmap.ic_launcher)
            .setContentText("My Song is playing").setContentTitle("Meri Duniya")
    .addAction(new Notification.Action(R.drawable.play,"Play",pIntent))
    .addAction(new Notification.Action(R.drawable.pause,"Pause",paIntent))
    .addAction(new Notification.Action(R.drawable.stop,"Stop",sIntent));



    Notification notification = builder.build();
    startForeground(9,notification);

}

I have also given permissions in manifest file.

标签: androidservicenullpointerexceptionandroid-pendingintentforeground-service

解决方案


你有这个:

Intent pause = new Intent(getApplicationContext(),MusicPlayerBoundService.class);
play.setAction("PAUSE");

PendingIntent paIntent = PendingIntent.getService(getApplicationContext(),100,pause,0);


Intent stop = new Intent(getApplicationContext(),MusicPlayerBoundService.class);
play.setAction("STOP");

您没有ACTION为 PAUSE 和 STOP 设置正确的变量。你需要这个:

Intent pause = new Intent(getApplicationContext(),MusicPlayerBoundService.class);
pause.setAction("PAUSE");

PendingIntent paIntent = PendingIntent.getService(getApplicationContext(),100,pause,0);


Intent stop = new Intent(getApplicationContext(),MusicPlayerBoundService.class);
stop.setAction("STOP");

推荐阅读