android.view.WindowInsets ClassNotFoundException

方人也*_*方人也 5 android classnotfoundexception

android.view.WindowInsets 在API级别20中添加.

我导入android.view.WindowInsets我的CustomLayout覆盖onApplyWindowInsets(WindowInsets insets),但ClassNotFoundException发生在一些手机,其api级别从14到21.这是什么原因?

发生在:Rooted 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
Run Code Online (Sandbox Code Playgroud)

Eug*_*nec 7

怎么了

系统遍历视图的所有公共方法并遇到被覆盖onApplyWindowInsetsWindowInsets参数.这种类型在系统中不存在因此崩溃.

Lollipop引入了该View.onApplyWindowInsets方法,但它也引入了OnApplyWindowInsetsListenerif,如果设置,则调用而不是上述方法.

什么时候发生

我在运行Android 4.4的三星设备上有这方面的报道.

它可以通过转储视图层次结构来触发.

怎么办呢

到目前为止,这并没有解决任何问题 救援来了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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

为什么会这样呢?

甘拜下风.


Jas*_*Jas 0

android.view.WindowInsets如果它是在 API 级别 20 中添加的,则无法在较低的 API 版本中使用。