无法通过此堆栈跟踪找到崩溃的原因

Bam*_*amx 13 service android view 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)
Run Code Online (Sandbox Code Playgroud)

.

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)
Run Code Online (Sandbox Code Playgroud)

似乎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;
}
Run Code Online (Sandbox Code Playgroud)

而我的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))
    );
}
Run Code Online (Sandbox Code Playgroud)

Bam*_*amx 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);
}
Run Code Online (Sandbox Code Playgroud)

我想在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
}
Run Code Online (Sandbox Code Playgroud)

这似乎解决了我的问题.我要感谢这篇文章的作者,它允许我找到这个修复程序):Android:更改WindowManager添加的ViewParams视图