Notification 不起作用

今天做Notification的时候不显示,很是郁闷。android 4.2 酷派手机什么也没提示,华为p9 android 6.0提示log如下:

 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Icon.getType()' on a null object reference

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Icon.getType()' on a null object reference
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.processSmallRightIcon(Notification.java:3337)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.applyStandardTemplate(Notification.java:3004)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.applyStandardTemplate(Notification.java:2980)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.makeContentView(Notification.java:3202)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.buildUnstyled(Notification.java:3384)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.build(Notification.java:3709)
。。。。。。

log上可以看出是空指针异常。在android 4.3源码 Notification.Builder中没有发现processSmallRightIcon()方法,应该是android6.0源码中有的方法。看这个方法名应该和smallIcon有关系。

代码如下:

NotificationManager notificationManager =  
	(NotificationManager)(mContext.getSystemService(Context.NOTIFICATION_SERVICE));
Notification.Builder builder= new Notification.Builder(mContext);
builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.icon_notification));
builder.setContentTitle(mContext.getText(R.string.glass_update));
String strProgress = mContext.getResources().getString(R.string.update_download_progress)+progress+"%";
builder.setContentText(strProgress);
Notification notification = builder.build();
notificationManager.notify(1, notification)
加上设置smallIcon的代码:

builder.setSmallIcon(R.drawable.icon_notification);
这样测试就没有问题了,Notification可以正常显示。



你可能感兴趣的:(Android,遇见的问题)