android自定义通知栏遇到的问题

自定义通知栏的布局时,为了复制了一个layout.xml文件,引用,但一直报错

android.app.RemoteServiceException: Bad notification posted from
 package com.example.notification: Couldn't expand RemoteViews 
for: StatusBarNotification(pkg=com.example.notification id=2 
tag=null score=0 notn=Notification(pri=0 contentView=com.example.notification/0x7f030001 vibrate=null 
sound=null defaults=0x0 flags=0x10 kind=[null]) 
user=UserHandle{0})

错误提示

         // 获得通知管理器
		NotificationManager nfManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		Notification myNotification = new Notification();
		// 提醒快讯
		myNotification.tickerText = "android:你好";
		// 设置通知时间
		myNotification.when = System.currentTimeMillis();
		// 设置图标
		myNotification.icon = R.drawable.ic_launcher;
		// 设置通知点击的动作
		Intent intent = new Intent(this, MainActivity.class);
		// 一个未来(预先)的动作,,用于点击或清除通知时的动作,
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				intent, 0);
		myNotification.contentIntent = pendingIntent;
		
		//设置远程视图
		RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
		
		myNotification.contentView = remoteViews;
		myNotification.flags = Notification.FLAG_AUTO_CANCEL;
		nfManager.notify(2,myNotification);

找了很久发现原因是:设置远程布局时,布局文件里面的按钮设置了静态的点击事件

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="showNotify"
        android:text="通知"/>

删除这个android:onClick="showNotify" 就可以运行了。

原文地址:https://www.cnblogs.com/zysun/p/5380744.html