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可以正常显示。



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值