35

I have a problem. Some times my service is forcefully closed with this logcat:

03-26 20:44:44.849: E/AndroidRuntime(12080): FATAL EXCEPTION: main
03-26 20:44:44.849: E/AndroidRuntime(12080): android.app.RemoteServiceException: Bad notification posted from package by.flipdev.vkspy: Couldn't expand RemoteViews for: StatusBarNotification(pkg=by.flipdev.vkspy id=1 tag=null score=0 notn=Notification(pri=0 contentView=by.flipdev.vkspy/0x1090071 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null]))
03-26 20:44:44.849: E/AndroidRuntime(12080):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at android.os.Looper.loop(Looper.java:137)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at android.app.ActivityThread.main(ActivityThread.java:4931)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at java.lang.reflect.Method.invokeNative(Native Method)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at java.lang.reflect.Method.invoke(Method.java:511)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-26 20:44:44.849: E/AndroidRuntime(12080):    at dalvik.system.NativeStart.main(Native Method)

This is my code to add add notifications:

protected void addNotification(final Bitmap Avatar,
        final int small_Image_ID, final int notify_id, final String text,
        final String title, final Boolean ongoing, final Boolean ticker,
        final String tickerText, final Boolean autoCancel,
        final String notificationCategory, final int notificationValue) {

    try {

        final Intent notificationIntent = new Intent(
                getApplicationContext(), CheckerActivity.class);

        notificationIntent
                .putExtra(notificationCategory, notificationValue);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        final PendingIntent contentIntent = PendingIntent.getActivity(
                getApplicationContext(), notify_id, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        final NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);


        final NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        if (Ticker) {
            builder.setContentIntent(contentIntent)
                    .setSmallIcon(small_Image_ID)
                    .setOngoing(ongoing)                    
                    .setLargeIcon(Avatar).setTicker(tickerText)                     
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(AutoCancel).setContentTitle(title)
                    .setContentText(text); // Текст уведомления
        } else {
            builder.setContentIntent(contentIntent)
                    .setSmallIcon(small_Image_ID)
                    .setLargeIcon(avatar)                       
                    .setOngoing(ongoing)
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(AutoCancel).setContentTitle(title) 
                    .setContentText(text); // Текст уведомления
        }

        final Notification n = builder.getNotification();

        nm.notify(notify_id, n);

    } catch (final Exception e) {
                    // TODO: add exception handling code
    }
}

Why is my service killed?

Jonik
  • 80,077
  • 70
  • 264
  • 372
user2212515
  • 1,220
  • 1
  • 12
  • 10
  • I don't know what's wrong, but maybe related to your avatar bitmap? Is it ever null? – Karim Varela Jul 02 '13 at 17:56
  • 1
    Can you make a check in your code if Avatar image is not null?
    For small_Image_ID where do you assign the value ?
    If the notification manager can't find a ressource it trigger that kind of message: Bad notification posted XXXX.
    So first thing to check is that all value does exist.
    Thx
    – Sistr Jul 22 '13 at 08:38
  • i'd say either avatar is null or small_Image_ID does not refer to a drawable. – njzk2 Sep 08 '13 at 23:11
  • possible duplicate of [android - Bad notification posted - Couldn't expand RemoteViews for: StatusBarNotification](http://stackoverflow.com/questions/6209631/android-bad-notification-posted-couldnt-expand-remoteviews-for-statusbarno) – kinghomer Sep 20 '13 at 14:17
  • seems like the avatar is not null: contentView=by.flipdev.vkspy/0x1090071 – Bush Oct 03 '13 at 21:35
  • I think getApplicationContext() is the problem here it may be null. – Biraj Zalavadia Oct 23 '13 at 09:04
  • please post your service implementation – Biraj Zalavadia Oct 23 '13 at 09:06
  • @user2212515, did you figure out the solution. I am still getting this exception , nothing worked for me for 4.4.2 . – Tasneem Dec 01 '16 at 06:02

4 Answers4

6

I noticed this happening on 3.0 emulators, when I set the Large Icon.

So, since the Large Icon is only used by 4.0+ devices, I solved this issue by checking if the Build version is > 13. If so (and only if so), I set the Large icon.

The problem has gone.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Very helpful! Indeed, `builder.setLargeIcon()` crashes on API level 12 (at least the emulator). (I've never been able to test on the level 13-14 emulators since they take most of an hour to start up, then crash before they're ready to do anything.) I previously found that `builder.setNumber()` also crashes on the API level 12 emulator. – Jerry101 Oct 27 '14 at 08:12
  • Well, nothing to worry about anymore. According to the [Dashboard](https://developer.android.com/about/dashboards/index.html) 3.x devices are now **less than the 0.1%** of the global market, so they won't even be anymore listed. Anyway, this is why I do set it only on devices > 13 (`if (Build.VERSION.SDK_INT > 13){...}`). – Phantômaxx Oct 27 '14 at 08:30
  • @Rotwang, I am getting this exception in 4.4.2 version. Not able to trace it. – Tasneem Dec 01 '16 at 06:02
5

This issue is a result of missing or null resources. Looking at your code, I infer that, possible error seems to be on Ticker (always false) and setLargeIcon(avatar)( avatar is always null).

Could you post all your service implementations?

P.S. Please try to respect java's naming convention; Ticker,Avatar,AutoCancel and other objects should start with lower case.

sud007
  • 5,824
  • 4
  • 56
  • 63
Anis BEN NSIR
  • 2,555
  • 20
  • 29
2

Well, in my case, I was getting this exact same error because I referred to a style definition for a TextView in my custom notification layout that had an entry for an onClick target method. Specifically:

<item name="android:onClick">onClick</item>

Once I removed that line, the problem went away. An oversight on my part, but a good reminder not to blindly reuse styles.

jkincali
  • 995
  • 1
  • 9
  • 10
0

Apart from recommended Layouts and Widgets by Google https://developer.android.com/reference/android/widget/RemoteViews

Check you layout.xml file and if you using android:tint property remove it. Because RemoteViews do not accept layout.xml with android:tint