2014-06-14 105 views
-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是不是一個應用程序資源。
  • 乾淨的構建並沒有幫助。

回答

0

我花了一段時間調試這一點。 NotificationCompat在這裏沒有完成它的工作。

解決方法:別叫builder.setNumber()在OS建立比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); 
} 

從理論上講,你可以調用builder.setContentInfo()代替,但對API V12沒有明顯的影響。

[我不能告訴如果bug還對API V13(HONEYCOMB_MR2)發生是因爲V13模擬器最需要一個小時的推出,然後嘎嘎叫着。每次。]

相關問題