创建 Android 通知时出现异常
Exception while creating an Android Notification
在我的应用程序中,我使用以下代码构建了一个通知:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_notify_nursing)
.setOngoing(true)
.setContentTitle(getString(R.string.feeding_in_progress));
if(android.os.Build.VERSION.SDK_INT >= 21) {
notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
}
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(resultPendingIntent);
if(android.os.Build.VERSION.SDK_INT >= 16) {
notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());
} else {
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.getNotification());
}
在 Google Play 控制台中,我看到为这一行记录了许多异常:
notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());
大部分错误发生在Android 5.0 和 5.1 中,旧版本没有问题。堆栈跟踪看起来像:
java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=1 dstPos=0 length=1
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3326)
at android.app.ActivityThread.access00(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1547)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=1 dstPos=0 length=1
at java.lang.System.arraycopy(System.java:358)
at android.util.ArrayMap.putAll(ArrayMap.java:579)
at android.os.Bundle.putAll(Bundle.java:183)
at android.app.Notification$Builder.build(Notification.java:3786)
知道这里发生了什么吗?
改用 NotificationCompat.Builder 可以使您的代码更简单并避免意外。
从 API 11 开始,该方法似乎已被弃用,如下所述..
Issue with Notification Manager on android
根据 notification guide list of required fields, you must set the content text via the setContentText() 方法。注意这里可以留空,但是需要设置
在我的应用程序中,我使用以下代码构建了一个通知:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_notify_nursing)
.setOngoing(true)
.setContentTitle(getString(R.string.feeding_in_progress));
if(android.os.Build.VERSION.SDK_INT >= 21) {
notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
}
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(resultPendingIntent);
if(android.os.Build.VERSION.SDK_INT >= 16) {
notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());
} else {
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.getNotification());
}
在 Google Play 控制台中,我看到为这一行记录了许多异常:
notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());
大部分错误发生在Android 5.0 和 5.1 中,旧版本没有问题。堆栈跟踪看起来像:
java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=1 dstPos=0 length=1
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3326)
at android.app.ActivityThread.access00(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1547)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=1 dstPos=0 length=1
at java.lang.System.arraycopy(System.java:358)
at android.util.ArrayMap.putAll(ArrayMap.java:579)
at android.os.Bundle.putAll(Bundle.java:183)
at android.app.Notification$Builder.build(Notification.java:3786)
知道这里发生了什么吗?
改用 NotificationCompat.Builder 可以使您的代码更简单并避免意外。
从 API 11 开始,该方法似乎已被弃用,如下所述..
Issue with Notification Manager on android
根据 notification guide list of required fields, you must set the content text via the setContentText() 方法。注意这里可以留空,但是需要设置