Android - java.lang.illegalargumentException: contentIntent خطأ مطلوب بسبب الإخطار؟

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

سؤال

لدي خدمة تعمل على تحديث إشعار في شريط الإخطار عندما يستلم رسالة تفيد بأنها يجب تغييرها.

ومع ذلك ، أحصل على الخطأ التالي في بعض الأحيان عندما يتم تحديث الإشعار

java.lang.IllegalArgumentException: contentIntent required

ها هو رمزتي:

إعداد متغير


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

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

NotificationManager mNotificationManager;

الإخطار بإنشاء


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

خلق الإخطار


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

تحديث الإخطار


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

إذن هناك شيء ما يحدث محتوىي في مكان ما على طول الخط ، هل سيكون ذلك صحيحًا؟

يتم إعلانه في الجزء العلوي من فئة الخدمة الخاصة بي كمتغير عضو ولا يتم استخدامه في أي مكان آخر في الكود بصرف النظر عن الموضح أعلاه ، فأين يمكن إعادة تعيينه إلى Null؟

هل كانت مفيدة؟

المحلول

تحتاج إلى تعيين المحتوى لإخطارك.

في حالتك:

notification.contentIntent = notificationIntent;

وإلا فإنك ستحصل على الرسالة ، وأن المحتوى من الإخطار لاغية ، لأنه لم يتم تعيينه.

Docu هنا: http://developer.android.com/reference/android/app/notification.html#contentent

لدي مثال صغير هنا: http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-download-from-android

نصائح أخرى

أعتقد أن هذا لأن إصدار نظام التشغيل Android

النسخة 2.3 أو أقل، يجب تعيين contentIntent، إن لم يكن ، ستحصل على هذا الاستثناء.

في مشروعي ، أكتب مثل هذا:

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; }

ربما هذا يمكن أن يساعدك!

في حالتك

contentIntent = pendingIntent.getActivity (هذا ، 0 ، إخطار ، 0) ؛

إذا كنت ترغب في استخدام النوايا مع نفس الإجراء ولكن الإضافات المختلفة:

1) التغيير

requestCode

من الافتراضي "0" في

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

لشيء فريد مثل `

(int) System.currentTimeMillis();

` 2)

الإخطار. contentIntent = الإخطار ؛

كلتا الخطوتين إلزاميان لأن:

  • الخيار 2 لن يعمل بدون الخيار 1.
  • الخيار 1 سوف يرمي غير unalfalArgumentException بدون 2.

في حالتي ، كان لدي رمز مثال للقيام به ، مع إخطار واحد لإنشاء ، كما حصلت على خطأ "ContentIntent مطلوب" - جلبت Google إلى هذا الموضوع: D

كان مصدر هذه المشكلة اقتباسات قمت بنسخها من رمز مثال ولصقها في مشروع Eclipse. عندما حذفت "" وكتبتها مرة أخرى وتم حل المشكلة. ربما هذا يساعد شخص ما.

كانت هذه اقتباسات مصدر الخطأ:nb.setContentTitle ("إخوتي الأول!") ؛ nb.setContentText ("Hello") ؛

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top