2014-06-14 4 views
-2

Lors du test d'une application sur les anciennes versions du système d'exploitation Android à l'aide de l'émulateur, elle se bloque sur l'API v12 avec un Resources$NotFoundException. Quelles sont les causes? Comment l'éviter?

java.lang.RuntimeException: Unable to start receiver com.onefishtwo.bbqtimer.AlarmReceiver: android.content.res.Resources$NotFoundException: Resource ID #0x1050019 
     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1908) 
     ... 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x1050019 
     at android.content.res.Resources.getValue(Resources.java:1014) 
     at android.content.res.Resources.getDimensionPixelSize(Resources.java:600) 
     at android.app.Notification$Builder.makeRemoteViews(Notification.java:938) 
     at android.app.Notification$Builder.makeContentView(Notification.java:954) 
     at android.app.Notification$Builder.getNotification(Notification.java:984) 
     at android.support.v4.app.NotificationCompatHoneycomb.add(NotificationCompatHoneycomb.java:52) 
     at android.support.v4.app.NotificationCompat$NotificationCompatImplHoneycomb.build(NotificationCompat.java:115) 
     at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:612) 
     at com.onefishtwo.bbqtimer.Notifier.openOrCancel(Notifier.java:156) 
     at com.onefishtwo.bbqtimer.AlarmReceiver.onReceive(AlarmReceiver.java:121) 
     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1901) 
     at android.app.ActivityThread.access$2400(ActivityThread.java:122) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1052) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:132) 
     at android.app.ActivityThread.main(ActivityThread.java:4025) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:491) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
     at dalvik.system.NativeStart.main(Native Method) 

Voici une version simplifiée du code openOrCancel():

NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 

builder.setSmallIcon(R.drawable.notification_icon) 
     .setContentTitle(context.getString(R.string.app_name)) 
     .setOngoing(true) 
     .setContentText(contentText) 
     .setNumber(numReminders); 

NotificationManager notificationManager = 
     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(NOTIFICATION_ID, builder.build()); // ***THROWS*** 
  • Il fonctionne très bien sur l'API Android OS v15 et versions ultérieures. Le but de NotificationCompat.Builder est de gérer la rétrocompatibilité.
  • L'ID de ressource # 0x1050019 n'est pas une ressource d'application.
  • Une construction propre n'aide pas.

Répondre

0

J'ai passé un moment à le déboguer. NotificationCompat ne fait pas son travail ici.

Solution: Ne pas appeler builder.setNumber() sur OS builds plus que l'API v15:

builder.setSmallIcon(R.drawable.notification_icon) 
     .setContentTitle(context.getString(R.string.app_name)) 
     .setOngoing(true) 
     .setContentText(contentText); 

// WORKAROUND a Resources$NotFoundException bug. 
if (Build.VERSION.SDK_INT >= 15) { 
    builder.setNumber(numReminders); 
} 

En théorie, vous pouvez appeler au lieu builder.setContentInfo(), mais qui n'a pas d'impact visible sur l'API v12.

[Je ne peux pas dire si le bogue survient également sur l'API v13 (HONEYCOMB_MR2) car l'émulateur v13 prend plus d'une heure à lancer, puis croque. Chaque fois.]

Questions connexes