-2
當測試舊Android操作系統的應用程序中使用模擬器版本,在API V12將其與Resources$NotFoundException
崩潰。這是什麼原因?如何避免它?
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)
下面是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***
- 它適用於Android操作系統的API V15和後面的微小。
NotificationCompat.Builder
的目的是處理向後兼容性。 - 資源ID#0x1050019是不是一個應用程序資源。
- 乾淨的構建並沒有幫助。