android - 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.
解决方案
你有这个:
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");
推荐阅读
- c# - Selenium .NET core 2.2 发布的项目 exe 即使在 Windows x64 上也无法运行
- vba - 当用户切换到下一个控件时,移动子窗体的代码或属性是什么?
- sql - 防止在批量查询中除以零
- php - 更改不同文件中的vs代码中的php代码
- python - Matplotlib 乳胶格式
- sql-server - SQL Server:如何始终查询名称中包含当前日期的表
- excel - 尝试从服务器位置 (VBA) 复制工作表时,如何防止“权限被拒绝”错误?
- apache-kafka - 在 Spring Cloud Stream 中使用嵌入式 Kafka 进行集成测试时,如何立即验证消息是否已得到确认?
- netty - 为什么在 netty 4.1 中不推荐使用 UDT 传输?
- python - 如何在seaborn boxplot中的同一子组之间创建间距?