膨胀类 com.google.android.material.slider.Slider 时出错

Posted

技术标签:

【中文标题】膨胀类 com.google.android.material.slider.Slider 时出错【英文标题】:Error inflating class com.google.android.material.slider.Slider 【发布时间】:2021-07-10 20:19:48 【问题描述】:

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

二进制 XML 文件第 9 行:二进制 XML 文件第 9 行:

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_
    android:layout_>

    <com.google.android.material.slider.Slider
        android:layout_
        android:layout_
        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 膨胀布局 warm.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 类:

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

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

更新

完整的堆栈跟踪在这里:

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$2>
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$2>
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$2.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$2.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$2.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: TypedValuet=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$2.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”版本”,我可以说由于与导航抽屉布局相关的渲染问题,根据 *** 中的一些说明,我将其更改为“材料:1.2.0-alpha02”并解决了渲染问题。 我在另一篇文章中问了这个问题。您可以在以下链接中访问该问题: https://***.com/questions/67095956/com-google-android-materialmaterial1-2-0-alpha02-does-not-include-some-instr

这里是 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>

【问题讨论】:

发布完整的堆栈跟踪 @Gabriele Mariotti 添加了完整的堆栈跟踪。提前谢谢你。 您使用的是 1.3.0 吗?你的应用主题是什么? @Gabriele Mariotti 不,我使用的是 1.2.0-alpha02 版本。 至少使用稳定版本 1.2.0 或 1.2.1 【参考方案1】:

我创建了以下主题:

<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_
    android:layout_
    android:theme="@style/MyMaterialTheme"/>

它有效。

【讨论】:

【参考方案2】:

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

【讨论】:

【参考方案3】:

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

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

【讨论】:

当我替换你的代码并运行我的应用程序时,我的应用程序没有被执行,它也没有给出任何错误。但是当我从 warm.XML 中删除提到的 XML 代码时,我得到了同样的错误。还是谢谢你。【参考方案4】:

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

【讨论】:

【参考方案5】:

这是由于您的基本应用主题不是材料主题的子主题。就我而言,我使用的是 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 视图级别使用它时准确地覆盖所需的主题。

【讨论】:

以上是关于膨胀类 com.google.android.material.slider.Slider 时出错的主要内容,如果未能解决你的问题,请参考以下文章

膨胀类 CollapsingToolbarLayout 时出错

膨胀类片段时出错

使用上下文类膨胀布局

如何为android修复“错误膨胀类GeckoEngineView”

在 RecyclerView 上膨胀类 ImageView 时出错

膨胀类 com.google.android.material.slider.Slider 时出错