Android - java.lang.IllegalArgumentException: contentIntent requis erreur provoquée par une notification?

StackOverflow https://stackoverflow.com/questions/3112008

Question

J'ai un service en cours d'exécution qui met à jour une notification dans la barre de notification quand il reçoit un message disant qu'il doit être changé.

Cependant, je reçois l'erreur suivante parfois lorsque la notification doit être mis à jour

java.lang.IllegalArgumentException: contentIntent required

Voici mon code:

Configuration variable


int icon = R.drawable.notification;
CharSequence tickerText = "Test";
long when = System.currentTimeMillis();
PendingIntent contentIntent;

Notification notification = new Notification(icon, tickerText, when);

NotificationManager mNotificationManager;

NotificationManager Création


    String ns = Context.NOTIFICATION_SERVICE;
    mNotificationManager = (NotificationManager) getSystemService(ns);

Création de notification


    Intent notificationIntent = new Intent(this, TestsApp.class);
    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.icon = R.drawable.notification3;
    notification.setLatestEventInfo(this, "Registering", "Test", contentIntent);
    mNotificationManager.notify(1, notification);

Mise à jour de la notification


    notification.icon = R.drawable.notification2;
    notification.setLatestEventInfo(getApplicationContext(), "Registered", "Test", contentIntent);
    mNotificationManager.notify(1, notification);   

Alors quelque chose se passe quelque part mon de contentIntent le long de la ligne, serait-ce pas?

Il est déclaré au sommet de ma classe de service en tant que variable membre et est utilisée nulle part ailleurs dans le code à part ci-dessus, alors où pourrait-il être remis à zéro devient?

Était-ce utile?

La solution

vous devez définir la contentIntent pour votre notification.

dans votre cas:

notification.contentIntent = notificationIntent;

sinon vous obtiendrez le message, que la contentIntent de la notification est nulle, parce que ce ne soit pas réglé.

le docu est ici: http://developer.android.com /reference/android/app/Notification.html#contentIntent

J'ai un petit exemple ici: http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android

Autres conseils

Je pense que cela est parce que le système d'exploitation Android version

La version 2.3 ou baissez , set doit contentIntent , sinon, vous obtiendrez cette exception.

Dans mon projet, j'écris comme ceci:

  

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { Intent intent = new Intent(); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); mNotification.contentIntent = contentIntent; }

Peut-être que cela pourrait vous aider!

Dans votre cas

  

contentIntent = PendingIntent.getActivity (ce, 0, notificationIntent,   0);

si vous voulez utiliser Intentions avec la même action, mais différents extras:

1) Changement

  

requestCode

de défaut "0" dans

  

getActivity (Context context, int requestCode, Intent intent, int flags)

pour quelque chose d'unique comme `

  

(int) System.currentTimeMillis();

` 2)

  

notification.contentIntent = notificationIntent;

Les deux étapes sont obligatoires parce que:

  • Option 2 ne fonctionnera pas sans l'option 1.
  • L'option 1 IllegalArgumentException sans jeter 2.

Dans mon cas, j'ai eu un exemple de code pour faire, avec une seule notification pour créer, et j'ai aussi obtenu « contentIntent nécessaire » erreur - Google m'a amené à ce fil: D

la source de ce problème sont des citations que j'ai copié à partir d'un exemple de code et collé dans projet Eclipse. Quand je supprimé « » et les dactylographié en arrière et problème a été résolu. Peut-être que cela aide quelqu'un.

Ces citations sont source d'erreur: nb.setContentTitle ( "Ma première notification!"); nb.setContentText ( "Bonjour");

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top