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" 就可以运行了。

  • 相关阅读:
    6.1.1.1 属性类型之数据属性
    6.1 理解对象
    5.7.2.4 random() 方法
    5.7.2.3 舍入方法
    5.7.2.2 min()和max()方法
    5.7.2.1 Math对象
    5.7.1.4 window对象
    frontend2_s9_part2_fe_d48_form
    s10_part3_django_basic.md
    frontend1_s9_part2_d47_html
  • 原文地址:https://www.cnblogs.com/zysun/p/5380744.html
Copyright © 2011-2022 走看看