我正在为 android 中的广播应用程序构建媒体样式通知。这是我的通知代码:
\n\nNotificationCompat.Action action = new android.support.v4.app.NotificationCompat.Action.Builder(imgNotificationAction, "playPause", pendingIntent).build();\n //create new notification\n NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)\n .setShowWhen(false)\n .setStyle(new NotificationCompat.MediaStyle()\n .setMediaSession(mediaSession.getSessionToken())\n .setShowActionsInCompactView(0, 1, 2))\n .setColor(ContextCompat.getColor(this, R.color.colorPrimary))\n //.setLargeIcon(largeIcon)\n .setSmallIcon(android.R.drawable.stat_sys_headset)\n .setContentText(radioName)\n .setContentTitle("Igala Radio presents")\n .setContentInfo("Igala language radio")\n .setContentIntent(pendingIntent)\n .addAction(action);\n\n NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);\n notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());\nRun Code Online (Sandbox Code Playgroud)\n\n但我在 Log cat 中遇到以下异常:
\n\nCaused by: java.lang.IllegalArgumentException: setShowActionsInCompactView: action 1 out of bounds (max 0)\n at android.app.Notification$MediaStyle.makeMediaContentView(Notification.java:4493)\n at android.app.Notification$MediaStyle.populateContentView(Notification.java:4427)\n at android.app.Notification$Style.buildStyled(Notification.java:3894)\n at android.app.Notification$MediaStyle.buildStyled(Notification.java:4415)\n at android.app.Notification$Builder.build(Notification.java:3638)\n at android.support.v4.app.NotificationCompatApi21$Builder.build(NotificationCompatApi21.java:132)\n at android.support.v7.app.NotificationCompat$LollipopExtender.build(NotificationCompat.java:484)\n at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:827)\n …Run Code Online (Sandbox Code Playgroud) android media-player android-notifications illegalstateexception