gpt4 book ai didi

android - 为通知膨胀约束布局时出错

转载 作者:行者123 更新时间:2023-12-04 02:02:22 31 4
gpt4 key购买 nike

我正在使用 RemoteViews 显示通知。当我使用 ConstraintLayout 时,发生膨胀错误。当我用 RelativeLayout 替换 ConstraintLayout 时,错误就解决了。
为什么 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)

我正在从服务发送通知。

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>

最佳答案

ConstraintLayout 不能用于远程 View 。可用于远程 View 的 View 和 View 组有限。请检查 https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

关于android - 为通知膨胀约束布局时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46273879/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com