在运行程序时碰到如下Info级日志,对于一个不能见黄、红、灰代码洁癖的人来说,这样的art日志是不能接受的。
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.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2463)
at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:938)
at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:806)
如果运行时设备低于28,则找不到该类,但运行时没有致命错误,程序不会崩溃。出现它的原因是低于 API 28的设备中,Android系统没有View#OnUnhandledKeyEventListener这个接口类,而28及以上引入了这个接口类。
解决的方案
如果实在不想看到警告告示,只能将compileSdkVersion降级到27及以下。
但是这种方案我不喜欢,我就要用高的SDK怎么了,怎么了,来打我呀。
我们先来认识下它
/**
* Interface definition for a callback to be invoked when a hardware key event hasn't
* been handled by the view hierarchy.
*/
public interface OnUnhandledKeyEventListener {
/**
* Called when a hardware key is dispatched to a view after being unhandled during normal
* {@link KeyEvent} dispatch.
*
* @param v The view the key has been dispatched to.
* @param event The KeyEvent object containing information about the event.
* @return {@code true} if the listener has consumed the event, {@code false} otherwise.
*/
boolean onUnhandledKeyEvent(View v, KeyEvent event);
}