我的应用程序的作用是显示通过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商店崩溃报告中获取的
以下是我将视图附加到系统窗口作为叠加层的方法:
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))
);
}
答案 0 :(得分:6)
惊人。我终于找到了问题的根源,但不是原因......我正在用我的观点做点什么:我想根据其方向改变其布局参数,所以我覆盖了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
类中发生了一些不好的事情,导致它无法正确处理重新附加,从而导致错误。
要解决此问题,只需通过正确的方法:使用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
}
这似乎解决了我的问题。我要感谢这篇文章的作者,它允许我找到这个修复程序):Android: change LayoutParams of View added by WindowManager
答案 1 :(得分:4)
我认为您在创建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
);
尝试改变这样,看看是否有效。
答案 2 :(得分:4)
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和ViewRootImpl
答案 3 :(得分:0)
Proguard会导致堆栈跟踪问题,尽管应该有一些你自己的代码,尽管需要进一步解读这些代码,所以这让我很难过,通过Gradle关闭Proguard,然后再次运行并查看。
答案 4 :(得分:0)
我在我的应用程序中遇到了同样的问题我使用
解决了问题public static final int TYPE_SYSTEM_ALERT;
而不是TYPE_SYSTEM_OVERLAY。我想在调用onMeasure()之前视图已经被破坏了。这种类型的叠加在某种程度上造成了一个问题,特别是没有命名的pre kit kat设备。
注意:如果您希望覆盖窗口显示在状态栏后面,并且仍然高于所有其他应用程序使用;
public static final int TYPE_PHONE;
我希望这会有所帮助。