通知中的 PendingIntent 不会恢复服务显示 java.lang.NullPointerException

问题描述

我创建了一个前台服务,因此使用 PendingIntents 实现了通知

通知也会显示

但是暂停和停止按钮不起作用。当我点击它时,应用程序崩溃。当我点击播放时,我的日志会被打印出来,这意味着它工作正常。

它返回的错误是这样的:

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) 

我的 onStartCommand() 方法

@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(),pause,0);


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

    PendingIntent sIntent = PendingIntent.getService(getApplicationContext(),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);

}

我还在清单文件中授予了权限。

解决方法

你有这个:

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

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

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

PendingIntent paIntent = PendingIntent.getService(getApplicationContext(),MusicPlayerBoundService.class);
stop.setAction("STOP");