在 Cardview 中膨胀二进制 xml 文件的类时出错

Posted

技术标签:

【中文标题】在 Cardview 中膨胀二进制 xml 文件的类时出错【英文标题】:Error inflating class of binary xml file in Cardview 【发布时间】:2020-08-30 05:41:02 【问题描述】:

这是我没有打开的 Java 文件

package com.impwala.impwala;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

public class hschome extends AppCompatActivity implements View.OnClickListener

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hschome);

        CardView card1 = findViewById(R.id.card1);
        CardView card2 = findViewById(R.id.card2);
        CardView card3 = findViewById(R.id.card3);
        CardView card4 = findViewById(R.id.card4);
        CardView card5 = findViewById(R.id.card5);

        card1.setOnClickListener(this);
        card2.setOnClickListener(this);
        card3.setOnClickListener(this);
        card4.setOnClickListener(this);
        card5.setOnClickListener(this);
    

    @Override
    public void onClick(View v) 
        switch (v.getId())
            case R.id.card1:
                openActivity();
                break;
            case R.id.card2:
                openActivity2();
                break;
            case R.id.card3:
                openActivity3();
                break;
            case R.id.card4:
                openActivity4();
                break;
            case R.id.card5:
                openActivity5();
                break;
        
    
    public void openActivity()
        Intent intent1=new Intent(this,Math.class);
        startActivity(intent1);
    
    public void openActivity2()
        Intent intent2=new Intent(this,Physics.class);
        startActivity(intent2);
    
    public void openActivity3()
        Intent intent3=new Intent(this,Biology.class);
        startActivity(intent3);
    
    public void openActivity4()
        Intent intent4=new Intent(this,Chemistry.class);
        startActivity(intent4);

    
    public void openActivity5() 
        Intent intent5;
        intent5 = new Intent(this, English.class);
        startActivity(intent5);
    

这是我的布局的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:layout_
    android:layout_
    android:background="@drawable/gradient"
    tools:context=".hschome">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:orientation="vertical"
        android:layout_
        android:layout_>


        <RelativeLayout
            android:layout_weight="2"

            android:layout_
            android:layout_>

            <TextView
                android:id="@+id/textGrid"
                android:layout_
                android:layout_
                android:layout_centerInParent="true"
                android:text="@string/hsc_board"
                android:textColor="#8b1a1a "
                android:textSize="36sp"
                android:textStyle="bold" />

        </RelativeLayout>


        <GridLayout
            android:id="@+id/mainGrid"
            android:rowCount="1"
            android:columnCount="1"
            android:alignmentMode="alignMargins"
            android:columnOrderPreserved="false"
            android:layout_weight="4"
            android:layout_
            android:layout_
            android:padding="30dp"
            >

            <!-- ROW 1  -->
            <!-- COLUMN 1  -->



            <androidx.cardview.widget.CardView
                android:id="@+id/card1"
                android:layout_
                android:layout_
                android:layout_columnWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_rowWeight="1"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="30dp">

                <RelativeLayout
                    android:layout_
                    android:layout_
                    android:background="@color/color1" />

                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_
                    android:layout_>


                    <TextView
                        android:textAlignment="center"
                        android:text="@string/mathematics"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_
                        android:layout_ />

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <!-- COLUMN 2  -->

            <androidx.cardview.widget.CardView
                android:id="@+id/card2"
                android:layout_
                android:layout_
                android:layout_columnWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_rowWeight="1"
                app:cardBackgroundColor="@drawable/gradient2"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="30dp">

                <RelativeLayout
                    android:layout_
                    android:layout_
                    android:background="@color/color1" />

                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_
                    android:layout_>


                    <TextView
                        android:textAlignment="center"
                        android:text="@string/physics"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_
                        android:layout_ />

                </LinearLayout>

            </androidx.cardview.widget.CardView>



            <!-- ROW 2  -->
            <!-- COLUMN 1  -->

            <androidx.cardview.widget.CardView
                android:id="@+id/card3"
                android:layout_
                android:layout_
                android:layout_columnWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_rowWeight="1"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="30dp">

                <RelativeLayout
                    android:layout_
                    android:layout_
                    android:background="@color/color1" />

                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_
                    android:layout_>


                    <TextView
                        android:layout_
                        android:layout_
                        android:text="@string/biology"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <!-- COLUMN 2  -->

            <androidx.cardview.widget.CardView
                android:id="@+id/card4"
                android:layout_
                android:layout_
                android:layout_columnWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_rowWeight="1"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="30dp">

                <RelativeLayout
                    android:layout_
                    android:layout_
                    android:background="@color/color1" />

                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_
                    android:layout_>


                    <TextView
                        android:layout_
                        android:layout_
                        android:text="@string/chemistry"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </androidx.cardview.widget.CardView>


            <!-- ROW 3  -->
            <!-- COLUMN 1  -->
            <androidx.cardview.widget.CardView
                android:id="@+id/card5"
                android:layout_
                android:layout_
                android:layout_columnWeight="1"
                android:layout_marginBottom="16dp"
                android:layout_rowWeight="1"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="30dp">

                <RelativeLayout
                    android:layout_
                    android:layout_
                    android:background="@color/color1" />

                <LinearLayout
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical"
                    android:layout_
                    android:layout_>


                    <TextView
                        android:textAlignment="center"
                        android:text="@string/english"
                        android:textColor="#000"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_
                        android:layout_ />

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <!-- COLUMN 2  -->



        </GridLayout>

    </androidx.appcompat.widget.LinearLayoutCompat>

</GridLayout>

这是我的应用程序的构建文件

apply plugin: 'com.android.application'

android 
    compileSdkVersion 29
    defaultConfig 
        applicationId "com.impwala.impwala"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 2
        versionName "1.1"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    
   buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        
    


dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'


    implementation 'androidx.core:core-ktx:1.5.0-alpha02'
    implementation "androidx.constraintlayout:constraintlayout:2.0.1"
    implementation 'androidx.recyclerview:recyclerview:1.1.0'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.2.0'

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'


    //add library
    implementation 'gr.pantrif:easy-android-splash-screen:0.0.1'
    implementation 'commons-io:commons-io:2.6'


    implementation 'androidx.cardview:cardview:1.0.0'




apply plugin: 'com.google.gms.google-services'

我在 Logcat 中遇到的错误非常困难并且没有得到解决。我不知道是什么问题。但是当我打开文件时它崩溃了。 它崩溃,消息显示来自布局和 java 文件链接的 java 文件行中的错误。行号在我的情况下,有 15 或 14 个东西。 我不知道问题是出在 onclicklistener 还是 AAndroidJUnit 还是不推荐使用的属性或 cardview 或其他任何东西上。

这是我遇到的错误

  java.lang.ClassNotFoundException: Didn't find class "androidx.core.app.CoreComponentFactory" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/com.tencent.ig--bBLUItSBvy1ZOEIt--4Tw==/lib/arm, /data/app/com.tencent.ig--bBLUItSBvy1ZOEIt--4Tw==/base.apk!/lib/armeabi-v7a, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.LoadedApk.createAppFactory(LoadedApk.java:228)
        at android.app.LoadedApk.updateApplicationInfo(LoadedApk.java:340)
        at android.app.ActivityThread.handleDispatchPackageBroadcast(ActivityThread.java:5497)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at com.android.server.SystemServer.run(SystemServer.java:474)
        at com.android.server.SystemServer.main(SystemServer.java:308)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:853)
2020-08-30 10:48:19.230 2893-2893/? E/LoadedApk: Unable to instantiate appComponentFactory
    java.lang.ClassNotFoundException: Didn't find class "androidx.core.app.CoreComponentFactory" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/com.tencent.ig--bBLUItSBvy1ZOEIt--4Tw==/lib/arm, /data/app/com.tencent.ig--bBLUItSBvy1ZOEIt--4Tw==/base.apk!/lib/armeabi-v7a, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.LoadedApk.createAppFactory(LoadedApk.java:228)
        at android.app.LoadedApk.updateApplicationInfo(LoadedApk.java:340)
        at android.app.ActivityThread.handleDispatchPackageBroadcast(ActivityThread.java:5497)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at com.android.server.SystemServer.run(SystemServer.java:474)
        at com.android.server.SystemServer.main(SystemServer.java:308)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:853)
2020-08-30 10:48:19.288 5838-5868/? E/MiuiSysImpl: READ_PHONE_STATE permission not granted
2020-08-30 10:48:19.347 5838-5857/? E/MiuiSysImpl: READ_PHONE_STATE permission not granted
2020-08-30 10:48:19.452 5904-5904/? E/ctivate.servic: Not starting debugger since process cannot load the jdwp agent.
2020-08-30 10:48:19.475 5863-5903/? E/##XLogger##: com.miui.cloudservice.cloudcontrol.@IPV6@SourceFile:2, thread:96480--cloud config is not expired , request canceled
2020-08-30 10:48:19.484 5926-5926/? E/com.xiaomi.xms: Not starting debugger since process cannot load the jdwp agent.
2020-08-30 10:48:19.638 5904-5904/? E/libc: Access denied finding property "ro.vendor.display.type"
2020-08-30 10:48:19.656 5948-5948/? E/.android.camer: Not starting debugger since process cannot load the jdwp agent.
2020-08-30 10:48:19.657 5926-5926/? E/libc: Access denied finding property "ro.vendor.display.type"
2020-08-30 10:48:19.863 5904-5942/? E/MiuiSysImpl: READ_PHONE_STATE permission not granted
2020-08-30 10:48:19.884 5948-5948/? E/libc: Access denied finding property "ro.vendor.display.type"
2020-08-30 10:48:20.006 5904-5931/? E/MiuiSysImpl: READ_PHONE_STATE permission not granted
2020-08-30 10:48:20.046 5948-5948/? E/libc: Access denied finding property "ro.boot.hwversion"
2020-08-30 10:48:20.108 5987-5987/? E/com.android.mm: Not starting debugger since process cannot load the jdwp agent.
2020-08-30 10:48:20.213 5948-6013/? E/CAM_WatermarkMiSysUtils: isFileExist NO Permission!!!!
2020-08-30 10:48:20.213 5948-6013/? E/CAM_WatermarkMiSysUtils: isFileExist NO Permission!!!!
2020-08-30 10:48:20.240 5987-5987/? E/com.android.mm: Dex checksum does not match for dex: /system/framework/com.google.android.maps.jar.Expected: 3079070930, actual: 1845989642
2020-08-30 10:48:20.320 5987-5987/? E/libc: Access denied finding property "ro.vendor.display.type"
2020-08-30 10:48:20.920 6095-6095/? E/iui.micloudsyn: Not starting debugger since process cannot load the jdwp agent.
2020-08-30 10:48:21.112 6095-6095/? E/libc: Access denied finding property "ro.vendor.display.type"
2020-08-30 10:48:21.125 5987-6083/? E/RcsService: initMaap
    java.lang.SecurityException: Failed to find provider chatbotinfo for user 10; expected to find a valid ContentProvider for this authority
        at android.os.Parcel.createException(Parcel.java:1953)
        at android.os.Parcel.readException(Parcel.java:1921)
        at android.os.Parcel.readException(Parcel.java:1871)
        at android.content.IContentService$Stub$Proxy.registerContentObserver(IContentService.java:849)
        at android.content.ContentResolver.registerContentObserver(ContentResolver.java:1988)
        at android.content.ContentResolver.registerContentObserver(ContentResolver.java:1977)
        at com.juphoon.helper.RcsChatbotManager.init(SourceFile:3)
        at com.xiaomi.rcs.tool.RcsService.initMaap(SourceFile:2)
        at com.xiaomi.rcs.tool.RcsService.access$500(SourceFile:1)
        at com.xiaomi.rcs.tool.RcsService$2.run(SourceFile:1)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.content.ContentService.registerContentObserver(ContentService.java:340)
        at android.content.IContentService$Stub.onTransact(IContentService.java:76)
        at com.android.server.content.ContentService.onTransact(ContentService.java:262)
        at android.os.Binder.execTransact(Binder.java:726)
2020-08-30 10:48:21.392 2893-3047/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
2020-08-30 10:48:22.044 6125-6125/? E/.android.camer: Not starting debugger since process cannot load the jdwp agent.
2020-08-30 10:48:22.167 6125-6125/? E/libc: Access denied finding property "ro.vendor.display.type"
2020-08-30 10:48:22.273 6125-6125/? E/libc: Access denied finding property "ro.boot.hwversion"
2020-08-30 10:48:22.435 6125-6142/? E/CAM_WatermarkMiSysUtils: isFileExist NO Permission!!!!
2020-08-30 10:48:22.435 6125-6142/? E/CAM_WatermarkMiSysUtils: isFileExist NO Permission!!!!
2020-08-30 10:48:22.973 5755-5755/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.impwala.impwala, PID: 5755
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.impwala.impwala/com.impwala.impwala.hschome: android.view.InflateException: Binary XML file line #92: Binary XML file line #92: Error inflating class <unknown>
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6861)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: android.view.InflateException: Binary XML file line #92: Binary XML file line #92: Error inflating class <unknown>
     Caused by: android.view.InflateException: Binary XML file line #92: Error inflating class <unknown>
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:651)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at com.impwala.impwala.hschome.onCreate(hschome.java:14)
        at android.app.Activity.performCreate(Activity.java:7232)
        at android.app.Activity.performCreate(Activity.java:7221)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6861)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
     Caused by: android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x7f07006e
        at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1054)
        at android.content.res.Resources.loadColorStateList(Resources.java:1055)
2020-08-30 10:48:22.973 5755-5755/? E/AndroidRuntime:     at android.content.res.TypedArray.getColorStateList(TypedArray.java:551)
        at androidx.cardview.widget.CardView.<init>(CardView.java:127)
        at androidx.cardview.widget.CardView.<init>(CardView.java:117)
            ... 32 more

请帮帮我。

【问题讨论】:

【参考方案1】:

检查drawble中的以下资源。错误可能存在于该梯度中或它自身不可用的资源中 android:background="@drawable/gradient"

【讨论】:

是的。这解决了我的问题。实际上渐变文件一切正常。但是当我尝试对渐变文件进行评论时,我发现我不小心将 app:cardBackgroundColor="@drawable/gradient2" 放入了我的一个卡片文件中。无论如何,谢谢。【参考方案2】:

尝试检查"@color/color1"是否已创建

【讨论】:

以上是关于在 Cardview 中膨胀二进制 xml 文件的类时出错的主要内容,如果未能解决你的问题,请参考以下文章

二进制 XML 文件第 13 行:膨胀类片段时出错

第 7 行的 InflateException 二进制 XML 文件:膨胀类片段时出错

二进制 XML 文件第 28 行:二进制 XML 文件第 28 行:膨胀类 <unknown> 时出错

Android:二进制 XML 文件行错误膨胀类

膨胀类 RecyclerView 时出错

二进制 XML 文件第 1 行:Xamarin Android 中 Google Maps 的类片段错误膨胀错误