Android:自定义曲面视图在方向更改时崩溃

时间:2013-09-25 00:37:44

标签: android xml surfaceview

我正在阅读有关如何在XML布局中使用自定义表面视图类的this tutorial,当我运行代码时,应用程序在我的手机方向发生变化时崩溃了。我注意到很多涉及自定义线程和表面视图子类的示例在方向发生变化时崩溃,是否有人知道为什么会发生这种情况?

09-25 18:19:40.639: E/Trace(4982): error opening trace file: No such file or directory (2)
09-25 18:19:40.639: D/ActivityThread(4982): setTargetHeapUtilization:0.25
09-25 18:19:40.639: D/ActivityThread(4982): setTargetHeapIdealFree:8388608
09-25 18:19:40.639: D/ActivityThread(4982): setTargetHeapConcurrentStart:2097152
09-25 18:19:40.959: D/libEGL(4982): loaded /system/lib/egl/libEGL_adreno200.so
09-25 18:19:40.979: D/libEGL(4982): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
09-25 18:19:40.979: D/libEGL(4982): loaded /system/lib/egl/libGLESv2_adreno200.so
09-25 18:19:41.049: I/Adreno200-EGLSUB(4982): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-25 18:19:41.099: E/(4982): <s3dReadConfigFile:75>: Can't open file for reading
09-25 18:19:41.099: E/(4982): <s3dReadConfigFile:75>: Can't open file for reading
09-25 18:19:41.099: D/OpenGLRenderer(4982): Enabling debug mode 0
09-25 18:19:58.127: W/dalvikvm(4982): threadid=11: thread exiting with uncaught exception (group=0x40d46438)
09-25 18:19:58.147: E/AndroidRuntime(4982): FATAL EXCEPTION: Thread-156822
09-25 18:19:58.147: E/AndroidRuntime(4982): java.lang.NullPointerException
09-25 18:19:58.147: E/AndroidRuntime(4982):     at com.example.practicesurface.MySurfaceView.onDraw(MySurfaceView.java:129)
09-25 18:19:58.147: E/AndroidRuntime(4982):     at com.example.practicesurface.MySurfaceView$MySurfaceThread.run(MySurfaceView.java:39)

1 个答案:

答案 0 :(得分:0)

移动这行代码:

thread = new MySurfaceThread(getHolder(), this);

从MySurfaceView中的init()方法到surfaceCreated()方法。

当移动方向时,会调用onCreate方法,但不会调用MySurfaceView的构造函数,因为它会被保留。

希望这会有所帮助:)