0

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

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)
4

1 回答 1

0

移动这行代码:

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

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

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

希望这可以帮助 :)

于 2013-09-25T01:12:13.957 回答