错误膨胀 class com.google.android.material.slider.Slider

Error inflating class com.google.android.material.slider.Slider

我想扩充包含单个 seekBar 的布局。通过这种方式,我创建了一个扩展片段 class 的 java class,然后我膨胀了布局。但出现以下错误:

Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.slider.Slider

warming.XML is in the following:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

    <com.google.android.material.slider.Slider
        android:layout_width="327dp"
        android:layout_height="37dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="42dp"
        android:layout_marginTop="217dp"
        android:layout_marginEnd="42dp"
        android:layout_marginBottom="349dp"
        android:valueFrom="0"
        android:valueTo="300"
        app:activeTrackColor="#ff4500"
        app:inactiveTrackColor="#fbb999" >

    </com.google.android.material.slider.Slider>
</RelativeLayout>

和 firstPage.java 膨胀布局 warming.XML 如下:

public class firstPage extends Fragment {
   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      return inflater.inflate(R.layout.warming,container,false);
   }
}

使用下面的代码,我想执行 firstPage.java class:

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new firstPage()).commit();

怎么了?谁能帮我?我在Whosebug和其他一些网站上搜索过这个问题,也有类似的问题,但没有人能解决我的问题。

更新

完整的堆栈跟踪在这里:

04-16 12:45:37.991 18832-18832/? I/art: Late-enabling -Xcheck:jni
04-16 12:45:37.991 18832-18832/? I/art: VMHOOK: rlim_cur : 0 pid:18832
04-16 12:45:38.132 18832-18832/com.example.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.myapplication-2/lib/arm
04-16 12:45:38.443 18832-18832/com.example.myapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-16 12:45:38.513 18832-18832/com.example.myapplication I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat>
04-16 12:45:38.513 18832-18832/com.example.myapplication I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat>
04-16 12:45:38.914 18832-18848/com.example.myapplication I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3__release_AU (I3fa967cfef)
    OpenGL ES Shader Compiler Version: E031.28.00.02
    Build Date: 09/29/15 Tue
    Local Branch: mybranch14683032
    Remote Branch: quic/master
    Local Patches: NONE
    Reconstruct Branch: NOTHING
04-16 12:45:51.663 18832-18832/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 18832
    android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class com.google.android.material.slider.Slider
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.myapplication.firstPage.onCreateView(firstPage.java:16)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:168)
        at android.app.ActivityThread.main(ActivityThread.java:5885)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.material.slider.Slider
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at com.example.myapplication.firstPage.onCreateView(firstPage.java:16) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) 
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) 
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439) 
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079) 
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869) 
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824) 
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:168) 
        at android.app.ActivityThread.main(ActivityThread.java:5885) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at android.view.LayoutInflater.createView(LayoutInflater.java:619)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at com.example.myapplication.firstPage.onCreateView(firstPage.java:16) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) 
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) 
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439) 
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079) 
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869) 
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824) 
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:168) 
        at android.app.ActivityThread.main(ActivityThread.java:5885) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 11: TypedValue{t=0x2/d=0x7f0300b4 a=3}
        at android.content.res.TypedArray.getColorStateList(TypedArray.java:482)
        at com.google.android.material.resources.MaterialResources.getColorStateList(MaterialResources.java:71)
        at com.google.android.material.slider.Slider.processAttributes(Slider.java:365)
        at com.google.android.material.slider.Slider.<init>(Slider.java:261)
        at com.google.android.material.slider.Slider.<init>(Slider.java:252)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at com.example.myapplication.firstPage.onCreateView(firstPage.java:16) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) 
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) 
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) 
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439) 
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079) 
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869) 
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824) 
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:168) 
        at android.app.ActivityThread.main(ActivityThread.java:5885) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
04-16 12:45:53.565 18832-18832/com.example.myapplication D/Process: killProcess, pid=18832
04-16 12:45:53.670 18832-18832/com.example.myapplication D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:113 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

下面是Build.gradle代码:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 22
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.0-alpha02'
//    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
//    implementation 'com.android.support:design:28.+'
//    implementation 'com.google.android.material:material:1.3.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

如果你想问我“你为什么不使用“material:1.3.0”版本”,我可以说是因为与导航抽屉布局相关的渲染问题,根据Whosebug 中的一些指令,我将其更改为“material:1.2.0-alpha02”,渲染问题就解决了。 我在另一个 post 中问这个问题。您可以在以下 link 中访问该问题:

这里是 Theme.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_200</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_200</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

您必须使用 app:trackColorActiveapp:trackColorInactive 而不是 app:activeTrackColorapp:inactiveTrackColor:

    <com.google.android.material.slider.Slider
        ...
        app:trackColorActive="#ff4500"
        app:trackColorInactive="#fbb999"/>

好吧,经过大量搜索,我终于发现在 android studio 中有一个与名为 的滑块类似的对象。因此,您可以使用此对象代替“滑块”对象。所以这种方式没有充气错误。

我创建了以下主题:

<style name="MyMaterialTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Add attributes here -->
    <item name="colorPrimary">@color/itimalia_pink_califoria</item>
</style>

然后:

   <com.google.android.material.slider.Slider
    android:id="@+id/slider"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:theme="@style/MyMaterialTheme"/>

而且有效。

我遇到了同样的问题。升级到 com.google.android.material:material:1.4.0 没有帮助。如上所述,唯一的解决方案是将 android:theme 设置为某个值,然后它起作用了。

这是因为您的基本应用主题不是 material 主题的子主题。在我的例子中,我使用的是 Appcompat child 的应用程序主题..但我使用的是一个继承了 MaterialComponenets 主题的所有内容的视图。它正在抛出错误。

应用主题:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
   ...
</style>

所以我创建了另一种样式,

<style name="WD_Slider" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
</style>

并在滑块视图中将该样式称为,

android:theme="@style/WD_Slider"

通过这种方法,您可以使用任何不是您的应用主题的子视图,并在您在视图级别 XML 中使用它时准确覆盖所需的主题。

如果您的应用程序主题不是源自 Material 设计,您可以通过将以下主题分配给 Slider 小部件来避免运行时崩溃。

android:主题="@style/Theme.MaterialComponents.DayNight.NoActionBar"