View $ OnUnhandledKeyEventListenerを調整する方法は?

プログラムを実行しているときに、次の情報レベルのログが発生しました。黄色、赤、灰色のコードのクリーン度を確認できない人にとって、このようなアートログは受け入れられません。

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);
    }

おすすめ

転載: blog.csdn.net/luo_boke/article/details/107866148