android.view.WindowInsets ClassNotFoundException [英] android.view.WindowInsets ClassNotFoundException
问题描述
android.view.WindowInsets
我导入了 CustomLayout
中> android.view.WindowInsets 并覆盖 onApplyWindowInsets(WindowInsets insets)
,但是 ClassNotFoundException
,这是什么原因?
I import android.view.WindowInsets
in my CustomLayout
and override onApplyWindowInsets(WindowInsets insets)
, but ClassNotFoundException
occurs in some phones, whose api level are from 14 to 21. What is the reason?
发生了上:
根植的Nexus 5,Android 4.4.2
Occurred on: 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
推荐答案
会发生什么
系统遍历所有公共对象视图的方法并遇到重写n onApplyWindowInsets
和 WindowInsets
参数。这种类型在系统中不存在,因此会导致崩溃。
What happens
The system traverses all public methods of a view and encounters the overridden onApplyWindowInsets
with WindowInsets
parameter. This type does not exist in the system hence the crash.
Lollipop引入了 View.onApplyWindowInsets
方法,但它也引入了 OnApplyWindowInsetsListener
,如果已设置,则会调用该方法,而不是上述方法。
Lollipop introduced the View.onApplyWindowInsets
method but it also introduced the OnApplyWindowInsetsListener
, which if set, is invoked instead of the aforementioned method.
我在运行Android 4.4的三星设备上有此报告。
I've had reports of this on Samsung devices running Android 4.4.
可以通过转储视图层次结构来触发。
It can be triggered by dumping view hierarchy.
到目前为止,这不能解决任何问题。为了解决这个问题,来了support-v4库:
So far this doesn't solve anything. To the rescue comes support-v4 library:
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库提供,因此它始终存在,不会直接在视图上公开任何不存在的将来类,并且该代码仅在Lollipop之后才有效(其中引入了实际的 WindowInsets
)。
Use the above in your common constructor. WindowInsetsCompat
is provided by the support-v4 library so it's always present, it does not expose any non-existent future classes directly on the view, and the code is effective only since Lollipop (where actual WindowInsets
were introduced).
打我。
这篇关于android.view.WindowInsets ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!