Using ViewGroup throws NullPointerException

2019-07-18 11:28发布

问题:

Does anyone have a clue what could be causing this?

11-16 16:23:26.745: ERROR/AndroidRuntime(9549): Uncaught handler: thread main exiting due to uncaught exception
11-16 16:23:26.765: ERROR/AndroidRuntime(9549): java.lang.NullPointerException
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at android.view.ViewRoot.performTraversals(ViewRoot.java:722)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at android.os.Looper.loop(Looper.java:123)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at java.lang.reflect.Method.invoke(Method.java:521)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
11-16 16:23:26.765: ERROR/AndroidRuntime(9549):     at dalvik.system.NativeStart.main(Native Method)

回答1:

This error used to show up in our crash reports with some regularity. It was very difficult to track down, but in the end we discovered that we sometimes triggered code that removed a view from the view hierarchy from surfaceDestroyed() (the callback method from SurfaceHolder). If your app is using a SurfaceView, make sure you don't do anything to modify the view hierarchy in surfaceDestroyed().