LoginSignup
3
1

More than 3 years have passed since last update.

カスタムViewのNotificationを持つアプリにAndroidXを導入した際に出たエラーと対応

Last updated at Posted at 2019-08-13

独自のViewを定義したNotificationを使用したAndroidアプリで、Support LibraryをAndroidXに置換えた時、次のようなエラーが出た。

08-14 01:02:03.603 28933-28933/[パッケージ名] E/AndroidRuntime: FATAL EXCEPTION: main
    android.app.RemoteServiceException: Bad notification posted from package [パッケージ名]: Couldn't expand RemoteViews for: StatusBarNotification(pkg=[パッケージ名] id=1 tag=null score=0 notn=Notification(pri=0 contentView=[パッケージ名]/0x7f0c006f vibrate=null sound=null defaults=0x0 flags=0x62 kind=[null]) user=UserHandle{0})
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:5092)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
        at dalvik.system.NativeStart.main(Native Method)

エラーの行も出ないので色々確認して調べたところ、NotificatioのレイアウトXMLにあった以下の定義が原因だった。

        <ImageView
            android:id="@+id/notificationAlbumArtImageView"
            android:layout_width="@android:dimen/notification_large_icon_width"
            android:layout_height="@android:dimen/notification_large_icon_height"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="4dp"
            android:layout_marginRight="4dp"
            android:adjustViewBounds="true"
            android:background="?attr/selectableItemBackground"
            android:clickable="false"
            android:contentDescription="@null"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/ic_media_default" />

android:backgroundを削除したところ、エラーは出なくなった。値として指定した?attr/selectableItemBackgroundが問題を起こしていた模様。
この値は元々Notification上では効果が無かったので意味は無かった。これは、昔色々試したときに入れた物がそのまま残ってたものが、今頃になってエラーとして顕在化してしまった。

教訓:余計なコードは削除する。残さない。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1