在 Android 4 中膨胀 class androidx.constraintlayout.widget.ConstraintLayout 时出错
Error inflating class androidx.constraintlayout.widget.ConstraintLayout in Android 4
我的应用程序在 Android 4.4 中展开布局时崩溃,错误是
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.ui.splashscreen.SplashScreen}: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
我已经使用了 androidx.constraintlayout:constraintlayout:2.0.0-beta3 或 androidx.constraintlayout:constraintlayout:1.1.3 但它仍然崩溃。
它仅在 android < 5 时崩溃,在 android >= 5
时运行良好
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
}
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/containers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient_primary"
tools:context=".ui.splashscreen.SplashScreen">
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/bg_splash"
app:tint="?attr/colorPrimaryDark" />
<ImageView
android:id="@+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo_izzy_white" />
</androidx.constraintlayout.widget.ConstraintLayout>
这很奇怪,因为它只在 Android < 5
时崩溃
已编辑
我发现是drawable导致的崩溃。我尝试使用支持库
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
和
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
在应用程序 class 中,但它仍然崩溃。
这是我的可绘制对象
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="315"
android:centerColor="?attr/theme_primary"
android:endColor="?attr/theme_primaryMiddle"
android:startColor="?attr/theme_primaryLight" />
</shape>
在build.gradle(应用级别)
中实施
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
希望它能奏效...
我发现我的崩溃是因为渐变,在棒棒糖之前的设备上它会导致崩溃,所以我创建了 2 个可绘制对象。第一个仅在 drawable 文件夹上是纯色,在 drawable-v24 上是渐变。
我的应用程序在 Android 4.4 中展开布局时崩溃,错误是
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.ui.splashscreen.SplashScreen}: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
我已经使用了 androidx.constraintlayout:constraintlayout:2.0.0-beta3 或 androidx.constraintlayout:constraintlayout:1.1.3 但它仍然崩溃。 它仅在 android < 5 时崩溃,在 android >= 5
时运行良好implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
}
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/containers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient_primary"
tools:context=".ui.splashscreen.SplashScreen">
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/bg_splash"
app:tint="?attr/colorPrimaryDark" />
<ImageView
android:id="@+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo_izzy_white" />
</androidx.constraintlayout.widget.ConstraintLayout>
这很奇怪,因为它只在 Android < 5
时崩溃已编辑
我发现是drawable导致的崩溃。我尝试使用支持库
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
和
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
在应用程序 class 中,但它仍然崩溃。 这是我的可绘制对象
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="315"
android:centerColor="?attr/theme_primary"
android:endColor="?attr/theme_primaryMiddle"
android:startColor="?attr/theme_primaryLight" />
</shape>
在build.gradle(应用级别)
中实施implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
希望它能奏效...
我发现我的崩溃是因为渐变,在棒棒糖之前的设备上它会导致崩溃,所以我创建了 2 个可绘制对象。第一个仅在 drawable 文件夹上是纯色,在 drawable-v24 上是渐变。