android.view.WindowInsets ClassNotFoundException 异常

标签 android classnotfoundexception

android.view.WindowInsets 添加到 API 级别 20。

我在我的 CustomLayout 中导入 android.view.WindowInsets 并覆盖 onApplyWindowInsets(WindowInsets insets),但是 ClassNotFoundException 出现在一些api级别为14到21的手机,请问是什么原因?

发生于: 根 Nexus 5,Android 4.4.2

堆栈跟踪:

Fatal Exception: java.lang.NoClassDefFoundError: android/view/WindowInsets
   at java.lang.Class.getDeclaredMethods(Class.java)
   at java.lang.Class.getDeclaredMethods(Class.java:656)
   at android.view.ViewDebug.getExportedPropertyMethods(ViewDebug.java:960)
   at android.view.ViewDebug.exportMethods(ViewDebug.java:1047)
   at android.view.ViewDebug.dumpViewProperties(ViewDebug.java:997)
   at android.view.ViewDebug.dumpViewProperties(ViewDebug.java:983)
   at android.view.ViewDebug.dumpView(ViewDebug.java:900)
   at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:855)
   at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
   at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
   at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
   at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
   at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
   at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
   at android.view.ViewDebug.dump(ViewDebug.java:793)
   at android.view.ViewDebug.dispatchCommand(ViewDebug.java:416)
   at android.view.ViewRootImpl$W.executeCommand(ViewRootImpl.java:6258)
   at android.view.IWindow$Stub.onTransact(IWindow.java:65)
   at android.os.Binder.execTransact(Binder.java:404)
   at dalvik.system.NativeStart.run(NativeStart.java)
Caused by java.lang.ClassNotFoundException: Didn't find class "android.view.WindowInsets" on path: DexPathList[[zip file "/data/app/***-1.apk"],nativeLibraryDirectories=[/data/app-lib/***-1, /vendor/lib, /system/lib]]
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
   at java.lang.Class.getDeclaredMethods(Class.java)
   at java.lang.Class.getDeclaredMethods(Class.java:656)
   at android.view.ViewDebug.getExportedPropertyMethods(ViewDebug.java:960)
   at android.view.ViewDebug.exportMethods(ViewDebug.java:1047)
   at android.view.ViewDebug.dumpViewProperties(ViewDebug.java:997)
   at android.view.ViewDebug.dumpViewProperties(ViewDebug.java:983)
   at android.view.ViewDebug.dumpView(ViewDebug

最佳答案

发生了什么

系统遍历一个view的所有public方法,遇到覆盖了onApplyWindowInsets参数的WindowInsets。系统中不存在此类型,因此导致崩溃。

Lollipop 引入了 View.onApplyWindowInsets 方法,但它也引入了 OnApplyWindowInsetsListener,如果已设置,将调用它而不是上述方法。

什么时候发生

我收到过有关运行 Android 4.4 的三星设备的报告。

它可以通过转储 View 层次结构来触发。

怎么办

到目前为止,这并没有解决任何问题。救援来了 support-v4 库:

public class SampleView extends View {
    public SampleView(final Context context) {
        this(context, null);
    }

    public SampleView(final Context context, @Nullable final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SampleView(final Context context, @Nullable final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(final View v, final WindowInsetsCompat insets) {
                // Do whatever you needed to do in the first place...
                return insets.consumeSystemWindowInsets();
            }
        });
    }
}

在您的通用构造函数中使用上面的内容。 WindowInsetsCompat 由 support-v4 库提供,因此它始终存在,它不会直接在 View 上公开任何不存在的 future 类,并且代码仅在 Lollipop 之后才有效(实际 引入了 WindowInsets)。

为什么会这样

打败了我。

关于android.view.WindowInsets ClassNotFoundException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35028395/

相关文章:

java - Android 设备选择器中的序列号/名称

android - 通过 adb 自动无线连接 Android 设备和 IntelliJ

java - 如何使用SharedPreference保存自定义ArrayList?

java - 从 oracle 过程调用 java 时出现 ClassNotFoundException

java - 是什么导致运行代码中的 java.lang.ClassNotFoundException

android - 从 ViewPager 更新 fragment

android - 在 onCreate() 和 onPostCreate() 之间

php - Kohana 2.3.4 + LINUX - ErrorException [ fatal error ] : Class not found

java - 是什么导致未引用的类出现 ClassNotFoundException?

java - 在 Maven 中运行时,JMeter 失败并出现 ClassNotFoundException