android 膨胀类时出错,

lyfkaqu1  于 2022-11-20  发布在  Android
关注(0)|答案(7)|浏览(140)

我想inflate一个包含单个seekBar的布局。通过这种方式,我创建了一个扩展fragment类的java类,然后我inflate了布局。但是发生了以下错误:
二进制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_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类:

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

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

更新

完整的堆栈跟踪如下:

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: 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$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”版本”,我可以说,因为与导航抽屉布局相关的渲染问题,根据StackOverFlow中的一些说明,我将其更改为“material:1.2.0-alpha 02”,渲染问题就解决了。我在另一篇文章中提出了这个问题。你可以在以下链接中访问这个问题:https://stackoverflow.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>
eit6fx6z

eit6fx6z1#

我创建了以下主题:

<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"/>

而且很管用。

7qhs6swi

7qhs6swi2#

好吧,经过大量的搜索,最后我发现有一个类似的对象滑块命名<androidx.appcompat.widget.AppCompatSeekBar>在android工作室.所以,你可以使用这个对象,而不是“滑块”对象.所以没有膨胀错误,在这种方式.

wj8zmpe1

wj8zmpe13#

这是因为您的基本应用主题不是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形式使用所需主题时完全覆盖该主题。

xmq68pz9

xmq68pz94#

如果您的应用程序主题不是从Material Design派生的,则可以通过将以下主题分配给Slider小部件来避免运行时崩溃。
android:theme="@style/主题.材料组件.白天夜晚.无动作栏”

js4nwp54

js4nwp545#

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

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

jdzmm42g6#

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

xyhw6mcr

xyhw6mcr7#

这给了我在Kotlin的精确追踪

seekBar?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seekBar: SeekBar, progress: Int,
                                       fromUser: Boolean) {
            
            tvProgress.text = "CAD "+ progress.toString()
            val bounds: Rect = seekBar.thumb.bounds
            tvProgress.x = (seekBar.left + bounds.left + seekBar.thumbOffset).toFloat()

         }

        override fun onStartTrackingTouch(seekBar: SeekBar) {
         }

        override fun onStopTrackingTouch(seekBar: SeekBar) {
            seekBarValue = seekBar.progress
         }
    })



<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">

            <TextView
                android:id="@+id/tvProgress"
                android:layout_width="@dimen/_50sdp"
                android:layout_height="@dimen/_30sdp"
                 android:background="@drawable/bg_box_border_unselected"
                android:backgroundTint="@color/black"
                android:fontFamily="@font/open_sans_regular"
                android:gravity="center"
                android:paddingStart="@dimen/_3sdp"
                android:paddingEnd="@dimen/_3sdp"
                android:text="CAD 20"
                android:textColor="@color/white" />

            <androidx.appcompat.widget.AppCompatSeekBar
                android:id="@+id/seekBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tvProgress"
                 android:max="28"
                android:maxHeight="@dimen/_5sdp"
                android:min="20"
                android:minHeight="@dimen/_5sdp"
                android:padding="@dimen/_10sdp"
                android:progress="20"
                android:progressDrawable="@drawable/custom_seekbar"
                android:thumb="@drawable/ic_thumb_seekbar" />
        </RelativeLayout>

相关问题