当我使用AndroidX时,无法打开使用Android支持库的第三方"对话片段"



我收到以下错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.co.XXX.XXX, PID: 22650
android.view.InflateException: Binary XML file line #10: Binary XML file line #4: Error inflating class com.google.android.material.appbar.AppBarLayout
Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class com.google.android.material.appbar.AppBarLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)

我在我的应用程序中使用以下代码

private void openCouponsListController() {
CouponsDialogFragment couponsDialogFragment = CouponsDialogFragment.newInstanceForList(CategoryId.FLIGHTS);
couponsDialogFragment.show(((AppCompatActivity)getActivity()).getSupportFragmentManager(), CouponsDialogFragment.class.getSimpleName());
}

当我的应用程序在 AndroidX 中时,CouponsDialogFragment使用支持库。我能够使用库中的活动,但无法弄清楚这个片段有什么问题。库的开发人员提供了一个活动,在 onCreate 中显示此对话框;它可以工作,但我无法从我的应用程序打开对话框。

片段正在导入以下库

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

布局文件如下所示

<com.google.android.material.appbar.AppBarLayout 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="wrap_content"
android:background="?attr/colorToolbarBg"
android:elevation="0dp"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/NuToolbarTheme"
app:navigationIcon="?attr/homeAsUpIndicator"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<!--todo :- need to verify we need this or not-->
<!--app:titleTextAppearance="@style/NuToolbarTitleTextAppearance"-->
</com.google.android.material.appbar.AppBarLayout>

已经在gradle.properties文件中设置了以下内容。

android.enableJetifier=true
android.useAndroidX=true

由于上述更改,布局文件和片段使用的是 AndroidX。在原始库中,它是支持库。

您可以使用 Jetifier 。Jetifier 工具迁移依赖于支持库的库,以改为依赖等效的 AndroidX 包。该工具允许您直接迁移单个库,而不是使用与 Android Studio 捆绑在一起的 Android gradle 插件。

要启用此功能,请将android.enableJetifier=true添加到您的 gradle.properties。

看到这个

<com.google.android.material.appbar.AppBarLayout 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="wrap_content"
android:background="?attr/colorToolbarBg"
android:elevation="0dp"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/NuToolbarTheme"
app:navigationIcon="?attr/homeAsUpIndicator"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<!--todo :- need to verify we need this or not-->
<!--app:titleTextAppearance="@style/NuToolbarTitleTextAppearance"-->
</com.google.android.material.appbar.AppBarLayout>

您在XML中使用了androidx组件,但是您使用的旧支持库的片段导致了问题。

首先使用所有第三方库设置您的项目,在构建成功后将项目迁移到 androidx

android.enableJetifier: When set to true,Android插件通过重写它们的二进制文件自动迁移现有的第三方库以使用AndroidX。如果未指定该标志,则默认情况下为 false。

最新更新