找不到类“ android.view.View()

提兵百万西湖上,立马吴山第一峰!这篇文章主要讲述找不到类“ android.view.View?相关的知识,希望能为你提供帮助。
【找不到类“ android.view.View()】我正在尝试使用充气机访问其他xml文件。

LayoutInflater inflater = (LayoutInflater)getBaseContext() .getSystemService(LAYOUT_INFLATER_SERVICE); View vi = inflater.inflate(R.layout.imagelist_item, null); TextView title = (TextView)vi.findViewById(R.id.description); title.setText("My Awesome Text");

但是我得到了错误“在路径:DexPathList上找不到类“ android.view.View $ OnUnhandledKeyEventListener””和“在先前失败的类java.lang.Class上拒绝重新初始化”
我不确定是什么原因引起的?
这是我的日志:
03/08 20:36:43: Launching 'app' on Nexus 5X API 26. Waiting for process to come online... Connected to process 4892 on device 'emulator-5554'. Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page. I/zygote: Rejecting re-init on previously-failed class java.lang.Class< androidx.core.view.ViewCompat$2> : java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener; at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2559) at void androidx.appcompat.widget.ActionBarContainer.< init> (android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62) at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2) at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334) at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:647) at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:790) I/zygote:at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:730) at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:863) at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:824) at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:515) at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:423) I/zygote:at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374) at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607) at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518) at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466) at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140) at void edu.cuhk.csci3310.cusweetnames.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:36) at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6975) at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1213) at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2770) at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2892) at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1) at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1593) I/zygote:at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:105) at void android.os.Looper.loop() (Looper.java:164) at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6541) at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2) at void com.android.internal.os.Zygote$MethodAndArgsCaller.run() (Zygote.java:240) at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767) Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/edu.cuhk.csci3310.cusweetnames-mm4Y3gQqoVOgcskW219QlQ==/base.apk"],nativeLibraryDirectories=[/data/app/edu.cuhk.csci3310.cusweetnames-mm4Y3gQqoVOgcskW219QlQ==/lib/x86, /system/lib, /vendor/lib]] at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:93) at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379) at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312) at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2559) at void androidx.appcompat.widget.ActionBarContainer.< init> (android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:62) at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2) at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334) at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:647) at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:790) I/zygote:at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:730) at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:863) at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:824) at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:515) at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:423) at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374) at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:607) at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518) I/zygote:at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466) at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140) at void edu.cuhk.csci3310.cusweetnames.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:36) at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6975) at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1213)

activity_main:
< ?xml version="1.0" encoding="utf-8"?> < androidx.constraintlayout.widget.ConstraintLayout 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_width="wrap_content" android:layout_height="match_parent" tools:context=".MainActivity"> < androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" /> < /androidx.constraintlayout.widget.ConstraintLayout>

imagelist_item.xml:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="6dp"> < RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/rl"> < ImageView android:id ="@+id/image" android:src="https://www.songbingjia.com/android/@drawable/p0_frozen_yogurt_milano_fresh_cafe" style="@style/image_title" /> < TextView android:id="@+id/description" android:textSize="17sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/image" android:layout_toRightOf="@+id/image"android:layout_alignTop="@+id/image" android:layout_marginStart="170dp" android:layout_marginLeft="197dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:layout_marginRight="-177dp" android:layout_marginBottom="10dp" android:text="@string/dessert_name" android:gravity="top" android:textColor="#000000" /> < TextView android:id="@+id/location" android:textSize="15sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/image" android:layout_toRightOf="@+id/image"android:layout_alignTop="@+id/image" android:layout_alignBottom="@+id/image" android:layout_marginStart="170dp" android:layout_marginLeft="197dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:layout_marginRight="-177dp" android:layout_marginBottom="10dp" android:gravity="center_vertical" android:text="@string/location" android:textColor="#000000" /> < TextView android:id="@+id/rating" android:textSize="15sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/image" android:layout_toRightOf="@+id/image" android:layout_alignBottom="@+id/image" android:layout_marginStart="170dp" android:layout_marginLeft="197dp" android:layout_marginTop="10dp" android:layout_marginEnd="10dp" android:layout_marginRight="-177dp" android:layout_marginBottom="10dp" android:gravity="bottom" android:text="@string/rating" android:textColor="#000000" android:drawableLeft="@drawable/star" android:drawablePadding="7dp" /> < /RelativeLayout> < /LinearLayout>

gradle(app):
apply plugin: 'com.android.application'android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { applicationId "edu.cuhk.csci3310.cusweetnames" minSdkVersion 15 targetSdkVersion 29 versionCode 1 versionName "1.0" 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.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' implementation 'androidx.recyclerview:recyclerview:1.1.0' }

答案问题是android.support.v4.view.ViewCompat并未在新的androidx包结构中实现View.OnUnhandledKeyEventListener,而仅从支持lib结构中的API 28(至少在版本28.0.0中)开始实现。因此,警告在API < 28的设备上显示,而在> = 28的设备上不显示。尝试将此注释添加到您的onCreate()
@RequiresApi(28) public void onCreate...

来自here但在Api28之前的设备上将无法使用]

    推荐阅读