使用模拟器测试旧版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***
NotificationCompat.Builder
的目的是处理向后兼容性。答案 0 :(得分:0)
我花了一段时间调试这个。 NotificationCompat
在这里没有做好工作。
解决方法:不要在早于API v15的操作系统版本上调用builder.setNumber()
:
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);
}
理论上,您可以改为调用builder.setContentInfo()
,但这对API v12没有明显影响。
[我无法判断这个错误是否也发生在API v13(HONEYCOMB_MR2)上,因为v13模拟器需要大约一个小时的时间才能启动,然后就会出现问题。每一次。]