java.lang.IllegalArgumentException: setShowActionsInCompactView: action 1 out of bounds (max 0)
Asked Answered
S

2

5

I am building media style notification for a radio app in android. Here's my code for notification :

NotificationCompat.Action action = new android.support.v4.app.NotificationCompat.Action.Builder(imgNotificationAction, "playPause", pendingIntent).build();
    //create new notification
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setShowWhen(false)
            .setStyle(new NotificationCompat.MediaStyle()
                    .setMediaSession(mediaSession.getSessionToken())
                    .setShowActionsInCompactView(0, 1, 2))
            .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
            //.setLargeIcon(largeIcon)
            .setSmallIcon(android.R.drawable.stat_sys_headset)
            .setContentText(radioName)
            .setContentTitle("Igala Radio presents")
            .setContentInfo("Igala language radio")
            .setContentIntent(pendingIntent)
            .addAction(action);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

But I get following exception in Log cat :

Caused by: java.lang.IllegalArgumentException: setShowActionsInCompactView: action 1 out of bounds (max 0)
                                                                 at android.app.Notification$MediaStyle.makeMediaContentView(Notification.java:4493)
                                                                 at android.app.Notification$MediaStyle.populateContentView(Notification.java:4427)
                                                                 at android.app.Notification$Style.buildStyled(Notification.java:3894)
                                                                 at android.app.Notification$MediaStyle.buildStyled(Notification.java:4415)
                                                                 at android.app.Notification$Builder.build(Notification.java:3638)
                                                                 at android.support.v4.app.NotificationCompatApi21$Builder.build(NotificationCompatApi21.java:132)
                                                                 at android.support.v7.app.NotificationCompat$LollipopExtender.build(NotificationCompat.java:484)
                                                                 at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:827)
                                                                 at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1744)
                                                                 at com.radio.igala.Service.PlayService.buildNotification(PlayService.java:253)
                                                                 at com.radio.igala.Service.PlayService.onStartCommand(PlayService.java:127)
                                                                 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3216)
                                                                 at android.app.ActivityThread.access$2200(ActivityThread.java:188) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1628) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                 at android.os.Looper.loop(Looper.java:210) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5839) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879) 

I am wondering why it states action 1 out of bounds (max 0). Does that mean I can't add any action in it?

Sprawl answered 21/12, 2017 at 7:42 Comment(0)
J
7

I think that error is happening because you are defining the actions (addAction()) after setShowActionsInCompactView().

This way, you are trying to setShowActionsInCompactView when notificationBuilder has no actions yet. In another words, its internal array has 0 elements (Max 0)

Can you try something like:

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
        .addAction(action)
        ...
        .setStyle(new NotificationCompat.MediaStyle()
                .setMediaSession(mediaSession.getSessionToken())
                .setShowActionsInCompactView(0, 1, 2))
        ....;

Edit

Another error I found is:

You are creating only one action:

NotificationCompat.Action action = new android.support.v4.app.NotificationCompat.Action.Builder(imgNotificationAction, "playPause", pendingIntent).build();

But here, you are setting 3 actions:

.setShowActionsInCompactView(0, 1, 2))

The number of actions setShowActionsInCompactView() should match with the number of actions you added via addAction().

You can add more actions (create new actions and call addAction() again or send only one argument to setShowActionsInCompactView()

Jurywoman answered 21/12, 2017 at 11:0 Comment(6)
Did exactly what you said. Still same exception :|Sprawl
@MoFaizanShaikh I updated my answer. Could you please double checkJurywoman
oh but I want to add only one action. what should I do?Sprawl
use .setShowActionsInCompactView(0) instead of .setShowActionsInCompactView(0, 1, 2)Jurywoman
Thank you that did the job :). Now, there arise another problem :|. when I start music within app it shows notification, but when I pause the music from notification and then play it again, then the notification disappearsSprawl
U helped alot. can you answer this plz?Sprawl
T
0
.setShowActionsInCompactView(0, 1, 2))

did the trick for me. @WormH0le

@WormH0le i love you.

Ton answered 23/8, 2022 at 14:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.