我正在尝试在通知按钮中设置onclick监听器,但是当我按下按钮时,我收到如下错误:
java.lang.RuntimeException: Unable to start service unnion.neelay.test1.MediaService@6fec2a5 with Intent { act=beatbox.neelay.dummybeat.ACTION_PLAY flg=0x10000000 cmp=unnion.neelay.test1/.MediaService bnds=[455,632][519,696] }: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.media.session.MediaControllerCompat$TransportControls.play()' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3034)
at android.app.ActivityThread.access$2200(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.media.session.MediaControllerCompat$TransportControls.play()' on a null object reference
at unnion.neelay.test1.MediaService.handleIncomingActions(MediaService.java:890)
at unnion.neelay.test1.MediaService.onStartCommand(MediaService.java:202)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3017)
at android.app.ActivityThread.access$2200(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
但是之前它工作正常这是错误中显示的方法。
private void handleIncomingActions(Intent playbackAction) {
if (playbackAction == null || playbackAction.getAction() == null) return;
String actionString = playbackAction.getAction();
if (actionString.equalsIgnoreCase(ACTION_PLAY)) {
transportControls.play();
} else if (actionString.equalsIgnoreCase(ACTION_PAUSE)) {
transportControls.pause();
} else if (actionString.equalsIgnoreCase(ACTION_NEXT)) {
// transportControls.skipToNext();
transportControls.skipToNext();
} else if (actionString.equalsIgnoreCase(ACTION_PREVIOUS)) {
// transportControls.skipToPrevious();
transportControls.skipToPrevious();
} else if (actionString.equalsIgnoreCase(ACTION_STOP)) {
// transportControls.stop();
transportControls.stop();
}
}
public PendingIntent playbackAction(int actionNumber) {
Intent playbackAction = new Intent(this, MediaService.class);
switch (actionNumber) {
case 0:
// Play
playbackAction.setAction(ACTION_PLAY);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
case 1:
// Pause
playbackAction.setAction(ACTION_PAUSE);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
case 2:
// Next track
playbackAction.setAction(ACTION_NEXT);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
case 3:
// Previous track
playbackAction.setAction(ACTION_PREVIOUS);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
case 4:
playbackAction.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
default:
break;
}
return null;
}
任何建议我做错了会有所帮助。
我称之为view.setOnClickPendingIntent(R.id.btnPause, playbackAction(1));
的方法
我试图让它在锁屏中工作,这就是为什么我将通知更改为前景,因为之前我正在覆盖onbackbutton,希望有人可以提前帮助我。