【问题标题】:Cant find the cause of my crash with this stack trace无法通过此堆栈跟踪找到我崩溃的原因
【发布时间】:2016-04-17 13:46:17
【问题描述】:

我的应用程序所做的是显示通过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)

似乎是操作系统 (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))
    );
}

【问题讨论】:

  • 你能告诉我你在这里添加哪个视图吗:windowManager.addView(this, params);
  • @KishuDroid 它只是一个扩展 View 的自定义类
  • @BamsBamx 我的回答是否阐明了您为什么会收到此错误?
  • 很高兴我能帮上忙 :)
  • 如果您在结束问题之前需要更多信息,请告诉我:)

标签: android service view stack-trace


【解决方案1】:

太棒了。我终于找到了我的问题的根源,但不是原因......我正在用我的观点做一些事情:我想根据它的方向改变它的布局参数,所以我覆盖了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

【讨论】:

    【解决方案2】:

    我认为您在创建 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
    );
    

    我也有referenced PixelFormat

    尝试像这样进行更改,看看是否有效。

    【讨论】:

    • 好吧,你的答案可能是对的。我会尝试一下,一旦我检查它是否有效,我会发布结果,谢谢。不过,我愿意接受新的答案;)
    • @BamsBamx,好的:)
    • 我终于找到了问题(我将在这个问题中作为答案发布),但是您的答案与我的问题无关,因为问题根源是另一个问题。还是谢谢你
    • @BamsBamx,恭喜。我试图根据您提供的信息找到问题。 :P
    【解决方案3】:

    当您在一连串调用 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。

    更多信息请查看WindowManagerGlobalViewRootImpl

    【讨论】:

      【解决方案4】:

      Proguard 可能会导致 Stack Trace 问题,虽然应该有你自己的代码的提示,即使进一步阅读需要破译,所以这让我很难过,通过 Gradle 关闭 Proguard,然后再次运行并查看。

      【讨论】:

      • 是的,你是对的。但是,事实并非如此,因为我已经测试过了,无法重现
      【解决方案5】:

      我在我的应用中遇到了同样的问题,我解决了这个问题

      public static final int TYPE_SYSTEM_ALERT; 
      

      而不是 TYPE_SYSTEM_OVERLAY。我猜在调用 onMeasure() 之前视图已经被破坏了。这种类型的覆盖以某种方式在特别是没有命名的 pre kit kat 设备上产生了问题。

      注意:如果您希望覆盖窗口显示在状态栏后面,并且仍然高于所有其他应用程序使用;

      public static final int TYPE_PHONE;
      

      我希望这会有所帮助。

      【讨论】:

      • 感谢您的回答,但它不适用于我的应用程序,因为它必须覆盖系统但让后面的窗口处理所有触摸。 'TYPE_SYSTEM_ALERT' 不会让那个
      • 不,它只允许使用正确的参数。 LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,是您需要的。请参阅文档: /** 窗口标志:即使此窗口可聚焦(其 * {@link #FLAG_NOT_FOCUSABLE} 未设置),也允许将窗口外的任何指针事件 * 发送到其后面的窗口。否则 * 它将消耗所有指针事件本身,无论它们是否在窗口内。 */ public static final int FLAG_NOT_TOUCH_MODAL = 0x00000020;
      猜你喜欢
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多