android实现常驻通知栏遇到的问题及解决办法

作者:唐人小调 时间:2022-10-12 06:06:58 

实现常驻通知栏时遇到的问题:

无论如何就是不显示通知,查看日志发现貌似报错了:

2020-06-28 14:11:34.923 6387-6387/xxx E/CrashReport: android.app.RemoteServiceException: Bad notification posted from package xxx: Couldn't inflate contentViewsandroid.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1944)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6815)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:818)

说的是布局报错,所使用的布局如下:

android实现常驻通知栏遇到的问题及解决办法

 根据报错信息来看,就是这个

android.support.constraint.ConstraintLayout

的问题了。

然后将布局的根view修改为RelativeLayout。

运行,报错,,,,纳尼?

2020-06-28 14:24:02.622 11436-11436/xxx E/CrashReport: android.app.RemoteServiceException: Bad notification posted from package xxx: Couldn't inflate contentViewsandroid.view.InflateException: Binary XML file line #2: Binary XML file line #2: You must supply a layout_height attribute.
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1944)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6815)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:818)

虽然继续报错,但是发现跟第一次报的错不同了,说明第一个报错问题解决。

那么来细看第二个报错信息:

Binary XML file line #2: You must supply a layout_height attribute.

看信息是说布局中缺少layout_height属性,但是确认布局中设置了这属性啊。。。


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="@dimen/dp_74"
 android:background="@drawable/shape_bg_resident_notify">

<ImageView
  android:id="@+id/iv_resident_weather"
  android:layout_width="@dimen/dp_45"
  android:layout_height="@dimen/dp_45"
  android:layout_marginStart="@dimen/dp_10"
  android:background="@mipmap/weather_icon_blue_big_cloudy"
  android:layout_centerVertical="true"/>

<TextView
  android:id="@+id/tv_resident_weather_temp"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="28"
  android:textSize="@dimen/sp_40"
  android:textColor="#ff333333"
  android:layout_toEndOf="@+id/iv_resident_weather"
  android:layout_centerVertical="true"
  android:layout_marginStart="@dimen/dp_5"/>

<TextView
  android:id="@+id/tv_resident_degree"
  android:layout_width="@dimen/dp_6"
  android:layout_height="@dimen/dp_6"
  android:layout_marginStart="@dimen/dp_3"
  android:layout_marginTop="@dimen/dp_24"
  android:layout_toEndOf="@+id/tv_resident_weather_temp"
  android:background="@drawable/shape_resident_weather_temp"/>

<TextView
  android:id="@+id/tv_resident_weather_cond"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="多云转晴"
  android:textSize="@dimen/sp_16"
  android:textColor="@color/color_333333"
  android:layout_marginTop="@dimen/dp_14"
  android:layout_marginStart="@dimen/dp_6"
  android:layout_toEndOf="@+id/tv_resident_degree" />

<TextView
  android:id="@+id/tv_resident_temp_range"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="21~34℃"
  android:textColor="@color/color_333333"
  android:textSize="@dimen/sp_16"
  android:layout_marginTop="@dimen/dp_5"
  android:layout_below="@+id/tv_resident_weather_cond"
  android:layout_toEndOf="@+id/tv_resident_degree"
  android:layout_marginStart="@dimen/dp_6"/>

<TextView
  android:id="@+id/tv_resident_aqi"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="空气优"
  android:textSize="@dimen/sp_16"
  android:textColor="@color/color_333333"
  android:layout_alignParentEnd="true"
  android:layout_marginTop="14dp"
  android:layout_marginEnd="@dimen/dp_10"/>

<ImageView
  android:id="@+id/iv_resident_aqi"
  android:layout_width="@dimen/dp_18"
  android:layout_height="@dimen/dp_18"
  android:src="@drawable/ic_icon_aqi"
  android:layout_toStartOf="@+id/tv_resident_aqi"
  android:layout_marginEnd="@dimen/dp_5"
  android:layout_marginTop="@dimen/dp_16"/>

<TextView
  android:id="@+id/tv_resident_desc"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="浦东新区 14:33发布"
  android:textSize="@dimen/sp_12"
  android:textColor="@color/color_999999"
  android:layout_marginEnd="@dimen/dp_10"
  android:layout_alignParentEnd="true"
  android:layout_below="@+id/tv_resident_aqi"
  android:layout_marginTop="@dimen/dp_11"/>

</RelativeLayout>

莫名奇妙啊简直

然后仔细想想可能的原因:难不成是因为分辨率适配的问题?

就是没有匹配到合适的分辨率的尺寸。那就试一下,把布局中所有引用@dimen的地方直接改为使用尺寸。

运行,成功!!!

android实现常驻通知栏遇到的问题及解决办法

问题:发现有个布局上的问题

自定义通知栏设置了背景,宽度是match_parent,但是发现在某些手机上,如小米6(截图所示),可以看到宽度竟然没有充满全屏。

但是在华为, vivo等手机上正常。

那就把这个背景去掉,自适应好了。

android实现常驻通知栏遇到的问题及解决办法

来源:https://blog.csdn.net/gengbaolong/article/details/106997947

标签:android,通知栏
0
投稿

猜你喜欢

  • Android通过ExifInterface判断Camera图片方向的方法

    2023-02-02 18:43:38
  • Android自定义属性 format的深入解析

    2022-12-05 14:56:57
  • Android编程连接MongoDB及增删改查等基本操作示例

    2023-11-28 14:49:16
  • Android实现京东上滑效果

    2021-10-11 07:27:11
  • 举例讲解Java中Piped管道输入输出流的线程通信控制

    2021-06-25 14:19:58
  • Spring+SpringMVC+JDBC实现登录的示例(附源码)

    2021-11-16 08:29:56
  • c#实现的操作oracle通用类

    2022-01-31 15:08:37
  • Java实现单向链表反转

    2023-11-18 01:03:11
  • C# Cache缓存读取的设置方法

    2022-11-18 05:33:53
  • Java探索之Feign入门使用详解

    2023-08-18 03:38:06
  • 详解c# AutoMapper 使用方式

    2022-05-25 20:51:38
  • 详解Android应用沙盒机制

    2023-01-30 10:43:37
  • 详解SpringBoot缓存的实例代码(EhCache 2.x 篇)

    2023-04-05 04:55:18
  • 关于weblogic部署Java项目的包冲突问题的解决

    2023-07-19 19:02:59
  • Java List转换成String数组几种实现方式详解

    2023-11-10 07:19:41
  • C#用链式方法表达循环嵌套

    2023-04-14 06:54:23
  • 详解java_ 集合综合案例:斗地主

    2022-02-08 04:14:49
  • 图文并茂讲解RocketMQ消息类别

    2023-06-11 07:59:41
  • Android通过Socket与服务器之间进行通信的示例

    2023-11-13 04:53:15
  • SWT(JFace)体验之复制粘贴

    2022-11-18 13:47:46
  • asp之家 软件编程 m.aspxhome.com