java.lang.reflect.InvocationTargetException - android.view.InflateException: Binary XML file line #1

Posted

技术标签:

【中文标题】java.lang.reflect.InvocationTargetException - android.view.InflateException: Binary XML file line #14: Error inflating class android.widget.TextView【英文标题】: 【发布时间】:2020-07-10 12:49:56 【问题描述】:

更新我的 android studio 版本后,出现此错误。 我没有更改代码中的任何内容,因此我不知道解决此问题的解决方案可能是什么。 在阅读了一些关于它的答案后,我已经完成了以下步骤:

    我将 - android.enableAapt2=false 添加到了我的 gradle.properties 我将此实现添加到我的应用程序中 - implementation 'com.android.support:design:28.0.0'

导致此错误的文本视图位于回收器视图内(这是卡片视图内的 xt 视图)

这是我的代码:

logcat-

2020-03-30 09:52:51.729 27874-27874/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-03-30 09:52:51.730 27874-27874/? E/Zygote: accessInfo : 1
2020-03-30 09:52:52.611 27874-27874/com.example.finalapp E/LoadedApk: Unable to instantiate appComponentFactory
    java.lang.ClassNotFoundException: Didn't find class "com.example.finalapp.whateverString" on path: DexPathList[[zip file "/data/app/com.example.finalapp-sFpoJsNpFHC7bAaU86nvow==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.finalapp-sFpoJsNpFHC7bAaU86nvow==/lib/arm64, /system/lib64, /system/vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.LoadedApk.createAppFactory(LoadedApk.java:239)
        at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:768)
        at android.app.LoadedApk.getClassLoader(LoadedApk.java:847)
        at android.app.LoadedApk.getResources(LoadedApk.java:1088)
        at android.app.ContextImpl.createAppContext(ContextImpl.java:2563)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6095)
        at android.app.ActivityThread.access$1200(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1785)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7045)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
2020-03-30 09:52:56.530 27874-27874/com.example.finalapp E/ViewRootImpl: sendUserActionEvent() returned.
2020-03-30 09:52:56.901 27874-27874/com.example.finalapp E/RecyclerView: No adapter attached; skipping layout
2020-03-30 09:52:58.404 27874-27874/com.example.finalapp E/AndroidRuntime: **FATAL EXCEPTION: main
    Process: com.example.finalapp, PID: 27874
    android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class android.widget.TextView
    Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class android.widget.TextView
    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:647)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.finalapp.DiveViewAdapter.onCreateViewHolder(DiveViewAdapter.java:30)
        at com.example.finalapp.DiveViewAdapter.onCreateViewHolder(DiveViewAdapter.java:13)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22420)
        at android.view.ViewGroup.layout(ViewGroup.java:6579)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
        at android.view.View.layout(View.java:22420)
        at android.view.ViewGroup.layout(ViewGroup.java:6579)
        at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
        at android.view.View.layout(View.java:22420)
        at android.view.ViewGroup.layout(ViewGroup.java:6579)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22420)
        at android.view.ViewGroup.layout(ViewGroup.java:6579)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22420)
        at android.view.ViewGroup.layout(ViewGroup.java:6579)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22420)
        at android.view.ViewGroup.layout(ViewGroup.java:6579)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22420)
        at android.view.ViewGroup.layout(ViewGroup.java:6579)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
2020-03-30 09:52:58.408 27874-27874/com.example.finalapp E/AndroidRuntime:     at com.android.internal.policy.DecorView.onLayout(DecorView.java:1041)
        at android.view.View.layout(View.java:22420)
        at android.view.ViewGroup.layout(ViewGroup.java:6579)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3355)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2819)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1865)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8488)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7045)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValuet=0x2/d=0x101009b a=1
        at android.content.res.TypedArray.getColorStateList(TypedArray.java:546)
        at android.widget.TextView.readTextAppearance(TextView.java:3888)
        at android.widget.TextView.<init>(TextView.java:1223)
        at android.widget.TextView.<init>(TextView.java:1112)
        at android.widget.TextView.<init>(TextView.java:1108)
            ... 70 more

文本视图的布局-

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardUseCompatPadding="true"
    app:contentPadding="10dp"
    app:cardCornerRadius="6dp"
    app:cardElevation="6dp"
    android:background="@color/colorPrimaryDark">


    <TextView
        android:layout_
        android:layout_
        android:id="@+id/number_of_dive"
        android:text="1"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="20sp"
        android:padding="5dp"/>

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:layout_gravity="center">

        <TextView
            android:layout_
            android:layout_
            android:id="@+id/card_dive_location"
            android:textSize="20sp"/>

        <TextView
            android:layout_
            android:layout_
            android:id="@+id/card_date_time"
            android:textSize="15sp"/>

    </LinearLayout>

</androidx.cardview.widget.CardView>

错误线-

package com.example.finalapp;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class DiveViewAdapter extends RecyclerView.Adapter<DiveViewHolder> 

    MainActivity mainActivity;
    ArrayList<Dive> diveArrayList;
    private DiveListener diveListener;

    public DiveViewAdapter(MainActivity mainActivity, ArrayList<Dive> diveArrayList,DiveListener diveListener) 
        this.mainActivity = mainActivity;
        this.diveArrayList = diveArrayList;
        this.diveListener=diveListener;
    

    @NonNull
    @Override
    public DiveViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 

        LayoutInflater layoutInflater= LayoutInflater.from(mainActivity.getBaseContext());
        ***View view = layoutInflater.inflate(R.layout.dive_logbook_card, parent, true);***

        return new DiveViewHolder(view,diveListener);
    

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull DiveViewHolder holder, int position) 

        holder.number_of_dive.setText((position+1)+"");
        holder.dive_location.setText(diveArrayList.get(position).getDive_site_location());
        holder.dive_date_time.setText("Date- "+diveArrayList.get(position).getDate_of_dive()+"   Time- "+
                diveArrayList.get(position).getTime_of_dive());



    

    public interface DiveListener 
        void onDiveClicked(int position, View view);
    

    DiveListener listener;

    public DiveListener getListener() 
        return listener;
    

    public void setListener(DiveListener listener) 
        this.listener = listener;
    

    @Override
    public int getItemCount() 
        return diveArrayList.size();
    

请帮帮我,我不知道还能做什么!

【问题讨论】:

您是否将您的课程添加到清单中? 什么课?????? 你根本没有 textview 还是现在没有? 我的应用中确实有文本视图,但问题只发生在回收视图中。 好的,我认为你的问题出在你的 recyclerview 中,而不是 textview 【参考方案1】:

如果您的 gradle 版本是 xxx-alphax 而不是尝试降级您的 gradle 版本,请同步并再次构建。

你也可以试试

mainActivity.getContext().getLayoutInflater();

而不是

LayoutInflater.from(mainActivity.getBaseContext());

【讨论】:

以上是关于java.lang.reflect.InvocationTargetException - android.view.InflateException: Binary XML file line #1的主要内容,如果未能解决你的问题,请参考以下文章