1

Ich erstelle Media Style Benachrichtigung für eine Radio-App in Android. Hier ist mein Code für die Benachrichtigung:java.lang.IllegalArgumentException: setShowActionsInCompactView: Aktion 1 außerhalb der Grenzen (max 0)

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

aber ich bekomme folgende Ausnahme in Log Katze:

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)  

Ich frage mich, warum es heißt Aktion 1 außerhalb der Grenzen (max 0). Bedeutet das, dass ich nichts hinzufügen kann?

Antwort

2

Ich denke, dass ein Fehler auftritt, weil Sie die Aktionen (addAction()) nach setShowActionsInCompactView() definieren.

Auf diese Weise versuchen Sie setShowActionsInCompactView, wenn notificationBuilder noch keine Aktionen hat. In anderen Worten, seine interne Array 0 Elemente (Max 0)

Können Sie versuchen, so etwas wie:

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

bearbeiten

Ein weiterer Fehler fand ich ist:

Sie sind Erstellen einer einzigen Aktion:

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

Aber hier, setzen Sie 3 Aktionen:

.setShowActionsInCompactView(0, 1, 2)) 

Die Anzahl der Aktionen setShowActionsInCompactView() sollten Sie über addAction() hinzugefügt mit der Anzahl der Aktionen entsprechen.

können Sie weitere Aktionen hinzufügen (neue Aktionen erstellen und addAction() erneut aufrufen oder nur ein Argument zu setShowActionsInCompactView()

+0

tat genau senden, was Sie gesagt noch gleiche Ausnahme: |. –

+0

@MoFaizanShaikh ich meine Antwort aktualisiert Könnten Sie bitte doppelt. Überprüfen Sie – W0rmH0le

+0

oh, aber ich möchte nur eine Aktion hinzufügen. Was soll ich tun? –

Verwandte Themen