zoukankan      html  css  js  c++  java
  • 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" 就可以运行了。

  • 相关阅读:
    图片和xml文件的转换
    WPF的样式(Style)继承
    .NET的序列化和反序列化
    WPF中的画板InkCanvas
    找到网页的源文件并找到歌曲文件的路径
    How to check if a ctrl + enter is pressed on a control?
    计算两个日期相差的天数
    图片保存到数据库以及从数据库中Load图片
    设计模式Command(命令模式)
    一个强大而且好用的UML设计工具
  • 原文地址:https://www.cnblogs.com/zysun/p/5380744.html
Copyright © 2011-2022 走看看