无法使用此堆栈跟踪找到我崩溃的原因
Cant find the cause of my crash with this stack trace
我的应用程序所做的是显示通过 windowManager.addView()
和 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
标志附加的系统覆盖视图。这是通过服务完成的,该服务管理视图可见性和其他一些事情。
但是,我收到了崩溃报告,但无法重现它们。此外,崩溃堆栈跟踪与我的应用程序包无关,所以我真的无法找到这个问题的根源。以下是来自不同来源的两个堆栈跟踪,但似乎是相关的:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.measure(int, int)' on a null object reference
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2388)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2101)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1297)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7011)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
.
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getMeasuredWidth()' on a null object reference
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2181)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1293)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6599)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5616)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
似乎 OS (ViewRootImpl) 导致了这个问题,因为它拥有对我的视图的空引用。所以我找不到解决方法。
自 4.4 以来的所有 Android 版本似乎都会发生这种情况,而且我的应用程序受到 Proguarded 保护。这些堆栈跟踪来自 Google Play 商店崩溃报告
以下是我如何将视图作为覆盖层附加到系统 window:
private void attachToSystemWindows(boolean overlayNavigationBar) {
final WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
final DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
final boolean isNavBarInBottom = isNavBarInBottom();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
calculateWindowWidth(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
calculateWindowHeight(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
0,
0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0x50728,
-3
);
params.gravity = Gravity.TOP;
windowManager.addView(this, params);
}
private boolean isNavBarInBottom() {
final boolean isLargeDevice = getResources().getConfiguration().smallestScreenWidthDp >= 600;
final int orientation = getResources().getConfiguration().orientation;
if (BuildConfig.DEBUG)
Log.d("MeshView", "Is NavBar in bottom: " + (isLargeDevice || orientation == Configuration.ORIENTATION_PORTRAIT));
return isLargeDevice || orientation == Configuration.ORIENTATION_PORTRAIT;
}
还有我的 onMeasure 方法:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (BuildConfig.DEBUG) Log.d("MeshView", "OnMeasure");
setMeasuredDimension(
Math.max(getSuggestedMinimumWidth(), resolveSize(SIZE_MIN_WIDTH, widthMeasureSpec)),
Math.max(getSuggestedMinimumHeight(), resolveSize(SIZE_MIN_HEIGHT, heightMeasureSpec))
);
}
Proguard 可能会导致 Stack Trace 问题,尽管应该有您自己的代码的提示,即使进一步阅读它需要破译,所以这让我很困惑,通过 Gradle 关闭 Proguard,然后 运行再看看。
我认为您在创建 params
时没有传递正确的标志。
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
calculateWindowWidth(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
calculateWindowHeight(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
0,
0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0x50728, // problem might be here.
-3
);
应该是these values之一。
所以像这样使用它(避免直接传递十六进制值):
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
calculateWindowWidth(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
calculateWindowHeight(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
0,
0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON, //any flag
PixelFormat.TRANSLUCENT
);
试试这样改,看看行不行。
我在我的应用程序中遇到了同样的问题我使用
解决了这个问题
public static final int TYPE_SYSTEM_ALERT;
而不是 TYPE_SYSTEM_OVERLAY。我想在调用 onMeasure() 之前视图已经被销毁了。这种类型的覆盖在某种程度上会在特别是没有命名的 pre kit kat 设备上产生问题。
注意:如果您希望叠加层 window 显示在状态栏后面并且仍然高于所有其他应用程序使用;
public static final int TYPE_PHONE;
希望对您有所帮助。
太棒了。我终于找到了问题的根源,但不是原因......我正在用我的观点做一些事情:我想根据它的方向改变它的布局参数,所以我覆盖了 onConfigurationChanged()
.
然后我使用了一种讨厌的方式来更新布局参数:分离然后再将其附加到 WindowManager
,就像这样:
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
final ViewGroup.LayoutParams layoutParams = getLayoutParams();
if (!(layoutParams instanceof WindowManager.LayoutParams)) return; //Fix for some ClassCastException in Samsung devices
final WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
final DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
final boolean isNavBarInBottom = isNavBarInBottom();
layoutParams.width = calculateWindowWidth(mOverlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight);
layoutParams.height = calculateWindowHeight(mOverlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight);
windowManager.removeView(this);
windowManager.addView(this, layoutParams);
}
我猜 ViewRootImpl
class 中发生了一些不好的事情,导致它无法正确处理重新连接,从而导致错误。
要解决这个问题,必须通过正确的方法:使用WindowManager.updateViewLayout();
方法:
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (BuildConfig.DEBUG) Log.d("MeshView", "OnConfigurationChanged");
final ViewGroup.LayoutParams layoutParams = getLayoutParams();
if (!(layoutParams instanceof WindowManager.LayoutParams)) return; //Fix for some ClassCastException in Samsung devices
final WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
final DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
final boolean isNavBarInBottom = isNavBarInBottom();
layoutParams.width = calculateWindowWidth(mOverlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight);
layoutParams.height = calculateWindowHeight(mOverlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight);
//windowManager.removeView(this); DON'T DO THIS
//windowManager.addView(this, layoutParams); DON'T DO THIS
windowManager.updateViewLayout(this, layoutParams); //DO THIS INSTEAD
}
这似乎解决了我的问题。我必须感谢此 post 的作者,是他让我找到了此修复程序):Android: change LayoutParams of View added by WindowManager
在一系列调用 ViewRoot.dispatchDetachedFromWindow() 之后通过 (windowManager.removeView(this)
) 删除视图时很短,这实际上将 viewroot 的视图设置为 null,这就是为什么在遍历时有 NPE .
详细信息:
通过
windowManager.removeView(this);
您实际上是在视图的 ViewRoot 上调用 die,然后它将 MSG_DIE
发送到其处理程序,以便稍后在安全时终止并将视图添加到 WindowManagerGlobal 中的 mDyingViews
列表。这一切都很好,
但是通过调用:
windowManager.addView(this, layoutParams);
您强制终止 ViewRoot 并调用 doDie()
并连续 ViewRoot.dispatchDetachedFromWindow()
在遍历时将视图设置为 null 并且您有 NPE。
查看更多信息WindowManagerGlobal and ViewRootImpl
我的应用程序所做的是显示通过 windowManager.addView()
和 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
标志附加的系统覆盖视图。这是通过服务完成的,该服务管理视图可见性和其他一些事情。
但是,我收到了崩溃报告,但无法重现它们。此外,崩溃堆栈跟踪与我的应用程序包无关,所以我真的无法找到这个问题的根源。以下是来自不同来源的两个堆栈跟踪,但似乎是相关的:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.measure(int, int)' on a null object reference
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2388)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2101)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1297)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7011)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
.
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getMeasuredWidth()' on a null object reference
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2181)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1293)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6599)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5616)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
似乎 OS (ViewRootImpl) 导致了这个问题,因为它拥有对我的视图的空引用。所以我找不到解决方法。
自 4.4 以来的所有 Android 版本似乎都会发生这种情况,而且我的应用程序受到 Proguarded 保护。这些堆栈跟踪来自 Google Play 商店崩溃报告
以下是我如何将视图作为覆盖层附加到系统 window:
private void attachToSystemWindows(boolean overlayNavigationBar) {
final WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
final DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
final boolean isNavBarInBottom = isNavBarInBottom();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
calculateWindowWidth(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
calculateWindowHeight(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
0,
0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0x50728,
-3
);
params.gravity = Gravity.TOP;
windowManager.addView(this, params);
}
private boolean isNavBarInBottom() {
final boolean isLargeDevice = getResources().getConfiguration().smallestScreenWidthDp >= 600;
final int orientation = getResources().getConfiguration().orientation;
if (BuildConfig.DEBUG)
Log.d("MeshView", "Is NavBar in bottom: " + (isLargeDevice || orientation == Configuration.ORIENTATION_PORTRAIT));
return isLargeDevice || orientation == Configuration.ORIENTATION_PORTRAIT;
}
还有我的 onMeasure 方法:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (BuildConfig.DEBUG) Log.d("MeshView", "OnMeasure");
setMeasuredDimension(
Math.max(getSuggestedMinimumWidth(), resolveSize(SIZE_MIN_WIDTH, widthMeasureSpec)),
Math.max(getSuggestedMinimumHeight(), resolveSize(SIZE_MIN_HEIGHT, heightMeasureSpec))
);
}
Proguard 可能会导致 Stack Trace 问题,尽管应该有您自己的代码的提示,即使进一步阅读它需要破译,所以这让我很困惑,通过 Gradle 关闭 Proguard,然后 运行再看看。
我认为您在创建 params
时没有传递正确的标志。
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
calculateWindowWidth(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
calculateWindowHeight(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
0,
0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
0x50728, // problem might be here.
-3
);
应该是these values之一。
所以像这样使用它(避免直接传递十六进制值):
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
calculateWindowWidth(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
calculateWindowHeight(overlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight),
0,
0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON, //any flag
PixelFormat.TRANSLUCENT
);
试试这样改,看看行不行。
我在我的应用程序中遇到了同样的问题我使用
解决了这个问题public static final int TYPE_SYSTEM_ALERT;
而不是 TYPE_SYSTEM_OVERLAY。我想在调用 onMeasure() 之前视图已经被销毁了。这种类型的覆盖在某种程度上会在特别是没有命名的 pre kit kat 设备上产生问题。
注意:如果您希望叠加层 window 显示在状态栏后面并且仍然高于所有其他应用程序使用;
public static final int TYPE_PHONE;
希望对您有所帮助。
太棒了。我终于找到了问题的根源,但不是原因......我正在用我的观点做一些事情:我想根据它的方向改变它的布局参数,所以我覆盖了 onConfigurationChanged()
.
然后我使用了一种讨厌的方式来更新布局参数:分离然后再将其附加到 WindowManager
,就像这样:
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
final ViewGroup.LayoutParams layoutParams = getLayoutParams();
if (!(layoutParams instanceof WindowManager.LayoutParams)) return; //Fix for some ClassCastException in Samsung devices
final WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
final DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
final boolean isNavBarInBottom = isNavBarInBottom();
layoutParams.width = calculateWindowWidth(mOverlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight);
layoutParams.height = calculateWindowHeight(mOverlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight);
windowManager.removeView(this);
windowManager.addView(this, layoutParams);
}
我猜 ViewRootImpl
class 中发生了一些不好的事情,导致它无法正确处理重新连接,从而导致错误。
要解决这个问题,必须通过正确的方法:使用WindowManager.updateViewLayout();
方法:
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (BuildConfig.DEBUG) Log.d("MeshView", "OnConfigurationChanged");
final ViewGroup.LayoutParams layoutParams = getLayoutParams();
if (!(layoutParams instanceof WindowManager.LayoutParams)) return; //Fix for some ClassCastException in Samsung devices
final WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
final DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
final boolean isNavBarInBottom = isNavBarInBottom();
layoutParams.width = calculateWindowWidth(mOverlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight);
layoutParams.height = calculateWindowHeight(mOverlayNavigationBar, isNavBarInBottom, metrics, mNavigationBarHeight);
//windowManager.removeView(this); DON'T DO THIS
//windowManager.addView(this, layoutParams); DON'T DO THIS
windowManager.updateViewLayout(this, layoutParams); //DO THIS INSTEAD
}
这似乎解决了我的问题。我必须感谢此 post 的作者,是他让我找到了此修复程序):Android: change LayoutParams of View added by WindowManager
在一系列调用 ViewRoot.dispatchDetachedFromWindow() 之后通过 (windowManager.removeView(this)
) 删除视图时很短,这实际上将 viewroot 的视图设置为 null,这就是为什么在遍历时有 NPE .
详细信息: 通过
windowManager.removeView(this);
您实际上是在视图的 ViewRoot 上调用 die,然后它将 MSG_DIE
发送到其处理程序,以便稍后在安全时终止并将视图添加到 WindowManagerGlobal 中的 mDyingViews
列表。这一切都很好,
但是通过调用:
windowManager.addView(this, layoutParams);
您强制终止 ViewRoot 并调用 doDie()
并连续 ViewRoot.dispatchDetachedFromWindow()
在遍历时将视图设置为 null 并且您有 NPE。
查看更多信息WindowManagerGlobal and ViewRootImpl