java.lang.ClassNotFoundException: Didn‘t find class “android.view.View$OnUnhandledKeyEventListener“

1.错误描述

 Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.rxtest-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.rxtest-2/lib/arm, /data/app/com.example.rxtest-2/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]

2. 错误原因

因为androidx从Android9.0(API 28)开始支持,如果项目使用了androidx但是运行在低于API 28版本的设备上就会出现;

3. 解决方案

无解,只要运行在API 28版本以上的设备就可以了;

注:

此错误也不好引起崩溃什么的,影响不大;

已标记关键词 清除标记
运行模拟器后,点击界面的任意控件都会弹出 很抱歉,”caihongpai“已停止运行 不知道这是为什么,求各位大神帮忙 logcat代码: ``` 03-09 07:50:14.924 3687-3687/? I/art: Late-enabling -Xcheck:jni 03-09 07:50:15.063 3687-3687/com.example.caihongpai 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 03-09 07:50:15.087 3687-3687/com.example.caihongpai I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper> 03-09 07:50:15.087 3687-3687/com.example.caihongpai I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper> 03-09 07:50:15.134 3687-3687/com.example.caihongpai D/AndroidRuntime: Shutting down VM 03-09 07:50:15.134 3687-3687/com.example.caihongpai E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.caihongpai, PID: 3687 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.caihongpai/com.example.caihongpai.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) at android.app.ActivityThread.access$800(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5305) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.caihongpai.MainActivity.onCreate(MainActivity.java:121) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)  at android.app.ActivityThread.access$800(ActivityThread.java:153)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5305)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  下面的是.java代码: package com.example.caihongpai; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button b1; ImageButton ib1,ib2,ib3; ImageView i1,i2,i3; TextView t1; static int[] s1={R.drawable.fangpian,R.drawable.heitao,R.drawable.hongtao}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); ib1=(ImageButton) findViewById(R.id.ib1); ib2=(ImageButton) findViewById(R.id.ib2); ib2=(ImageButton) findViewById(R.id.ib2); i1=(ImageView) findViewById(R.id.i1); i2=(ImageView) findViewById(R.id.i2); i3=(ImageView) findViewById(R.id.i3); ib1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ib1.setImageResource(s1[0]); ib2.setImageResource(s1[1]); ib3.setImageResource(s1[2]); if(s1[0]==R.drawable.hongtao){ new AlertDialog.Builder(MainActivity.this) .setTitle("奥利给") .setMessage("小伙子,你答对了,真是个天才!!") .setPositiveButton("确认",null) .show(); i3.setVisibility(View.VISIBLE); ib1.setVisibility(View.INVISIBLE); ib2.setVisibility(View.INVISIBLE); ib3.setVisibility(View.INVISIBLE); }else{ new AlertDialog.Builder(MainActivity.this) .setTitle("向我开炮") .setMessage("小傻瓜,你答错了,真是个小笨蛋!!") .setPositiveButton("确认",new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialogInterface, int which) { return; } }) .setNegativeButton("再试一次", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { xp(); } }) .show(); i3.setVisibility(View.VISIBLE); ib1.setVisibility(View.INVISIBLE); ib2.setVisibility(View.INVISIBLE); ib3.setVisibility(View.INVISIBLE); } } }); ib2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ib1.setImageResource(s1[0]); ib2.setImageResource(s1[1]); ib3.setImageResource(s1[2]); if(s1[1]==R.drawable.hongtao){ new AlertDialog.Builder(MainActivity.this) .setTitle("奥利给") .setMessage("小伙子,你答对了,真是个天才!!") .setPositiveButton("确认",null) .show(); i3.setVisibility(View.VISIBLE); ib1.setVisibility(View.INVISIBLE); ib2.setVisibility(View.INVISIBLE); ib3.setVisibility(View.INVISIBLE); }else{ new AlertDialog.Builder(MainActivity.this) .setTitle("向我开炮") .setMessage("小傻瓜,你答错了,真是个小笨蛋!!") .setPositiveButton("确认",new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialogInterface, int which) { return; } }) .setNegativeButton("再试一次", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { xp(); } }) .show(); i3.setVisibility(View.VISIBLE); ib1.setVisibility(View.INVISIBLE); ib2.setVisibility(View.INVISIBLE); ib3.setVisibility(View.INVISIBLE); } } }); ib3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ib1.setImageResource(s1[0]); ib2.setImageResource(s1[1]); ib3.setImageResource(s1[2]); if(s1[2]==R.drawable.hongtao){ new AlertDialog.Builder(MainActivity.this) .setTitle("奥利给") .setMessage("小伙子,你答对了,真是个天才!!") .setPositiveButton("确认",null) .show(); i3.setVisibility(View.VISIBLE); ib1.setVisibility(View.INVISIBLE); ib2.setVisibility(View.INVISIBLE); ib3.setVisibility(View.INVISIBLE); }else{ new AlertDialog.Builder(MainActivity.this) .setTitle("向我开炮") .setMessage("小傻瓜,你答错了,真是个小笨蛋!!") .setPositiveButton("确认",new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialogInterface, int which) { return; } }) .setNegativeButton("再试一次", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { xp(); } }) .show(); i3.setVisibility(View.VISIBLE); ib1.setVisibility(View.INVISIBLE); ib2.setVisibility(View.INVISIBLE); ib3.setVisibility(View.INVISIBLE); } } }); } private void xp() { for (int i = 0; i < 3; i++) { int tmp = s1[i]; int s = (int) (Math.random() * 2); s1[i] = s1[s]; s1[s] = tmp; } } } 下面的是xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#0CC6BB" tools:context=".MainActivity"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="猜猜红桃A在哪" android:textSize="30dp" android:gravity="center" android:layout_marginTop="50dp" /> <FrameLayout android:layout_width="match_parent" android:layout_height="180dp" android:layout_margin="10dp" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="180dp" android:orientation="horizontal"> <ImageButton android:id="@+id/ib1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:foregroundGravity="top" app:srcCompat="@drawable/beimian" /> <ImageButton android:id="@+id/ib2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:foregroundGravity="top" app:srcCompat="@drawable/beimian" /> <ImageButton android:id="@+id/ib3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:foregroundGravity="top" app:srcCompat="@drawable/beimian" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="180dp" android:orientation="horizontal"> <ImageView android:id="@+id/i1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" app:srcCompat="@drawable/heitao" android:visibility="invisible"/> <ImageView android:id="@+id/i2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" app:srcCompat="@drawable/fangpian" android:visibility="invisible"/> <ImageView android:id="@+id/i3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" app:srcCompat="@drawable/hongtao" android:visibility="invisible" /> </LinearLayout> </FrameLayout> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="让我再玩一次噻" android:textSize="20dp" /> </LinearLayout> ```
©️2020 CSDN 皮肤主题: 点我我会动 设计师:上身试试 返回首页