我正在使用RemoteViews来展示通知。当我使用ConstraintLayout时,会出现一个膨胀错误。当我将ConstraintLayout替换为RelativeLayout时,错误得到解决。
为什么ConstraintLayout不能被膨胀?
在运行时我遇到了以下错误:
错误
Couldn't inflate view for notification com.inadev.datasync.example/0x1
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.constraint.ConstraintLayout" on path: DexPathList[[],nativeLibraryDirectories=[/system/priv-app/MotCamera/lib/arm, /system/priv-app/MotCamera/MotCamera.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:609)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.widget.RemoteViews.inflateView(RemoteViews.java:3235)
at android.widget.RemoteViews.apply(RemoteViews.java:3199)
at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1711)
at com.android.systemui.statusbar.BaseStatusBar.createNotificationViews(BaseStatusBar.java:2279)
at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:1579)
at com.android.systemui.statusbar.BaseStatusBar$7$2.run(BaseStatusBar.java:661)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
09-18 12:07:59.589 6905-6905/com.inadev.datasync.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.inadev.datasync.example, PID: 6905
android.app.RemoteServiceException: Bad notification posted from package com.inadev.datasync.example: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.inadev.datasync.example user=UserHandle{0} id=1 tag=null key=0|com.inadev.datasync.example|1|null|10203: Notification(pri=0 contentView=com.inadev.datasync.example/0x7f040030 vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
I am sending Notification form the service.
Java
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("dataSync_channel","DataSync Notification Channel",NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("DataSync unsynchronized data status");
channel.setVibrationPattern(new long[] {100,200,300,400});
channel.enableLights(false);
channel.enableVibration(true);
mNotificationManager.createNotificationChannel(channel);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this,channelId)
.setContentTitle("DataSync Status")
.setSmallIcon(android.R.mipmap.sym_def_app_icon);
int assignmentCount=0;
int screenCount=0;
int attachmentCount=0;
int fieldCount=0;
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_unsync_data);
remoteViews.setTextViewText(R.id.text_assignment_count,assignmentCount + "");
remoteViews.setTextViewText(R.id.text_screen_count, screenCount + "");
remoteViews.setTextViewText(R.id.text_field_count, fieldCount + "");
remoteViews.setTextViewText(R.id.text_attachment_count, attachmentCount + "");
mBuilder.setContent(remoteViews);
mNotificationManager.notify(1, mBuilder.build());
这个布局文件被用来填充通知内容。
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Assignment: "
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen: "
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field: "
android:layout_marginTop="7dp"
app:layout_constraintTop_toBottomOf="@+id/textView2"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attachment: "
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView3"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<TextView
android:id="@+id/text_assignment_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/textView"
android:layout_marginLeft="32dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp" />
<TextView
android:id="@+id/text_screen_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/textView2"
android:layout_marginLeft="32dp"
app:layout_constraintHorizontal_bias="0.13"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/text_assignment_count" />
<TextView
android:id="@+id/text_field_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/textView3"
android:layout_marginLeft="32dp"
app:layout_constraintHorizontal_bias="0.18"
android:layout_marginTop="6dp"
app:layout_constraintTop_toBottomOf="@+id/text_screen_count" />
<TextView
android:id="@+id/text_attachment_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/textView4"
android:layout_marginLeft="31dp"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/text_field_count" />
</android.support.constraint.ConstraintLayout>
RelativeLayout
被注释为android.widget.RemoteViews.RemoteView
- 文档中说:“此注释表示View的子类可以与RemoteViews机制一起使用。” - pskink