安卓定制表面观崩溃后方向改变 [英] Android: Custom Surface View crashing upon orientation change

查看:206
本文介绍了安卓定制表面观崩溃后方向改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读本教程如何在一个XML布局使用自定义表面观类,当我跑了code,应用程序崩溃时,我的手机的方向改变。我注意到很多涉及定制的螺纹和表面观子类时,方向更改崩溃的例子,没有任何人有任何想法,为什么发生这种情况?

  09-25 18:19:40.639:E /跟踪(4982):错误打开跟踪文件:没有这样的文件或目录(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):加载/system/lib/egl/libEGL_adreno200.so
09-25 18:19:40.979:D / libEGL(4982):加载/system/lib/egl/libGLESv1_CM_adreno200.so
09-25 18:19:40.979:D / libEGL(4982):加载/system/lib/egl/libGLESv2_adreno200.so
09-25 18:19:41.049的:I /的Adreno200-EGLSUB(4982):或其可ConfigWindowMatch:2087计算值:格式RGBA_8888。
09-25 18:19:41.099:E /(4982):其中,s3dReadConfigFile:75计算值:无法打开文件进行读取
09-25 18:19:41.099:E /(4982):其中,s3dReadConfigFile:75计算值:无法打开文件进行读取
09-25 18:19:41.099:D / OpenGLRenderer(4982):启用调试模式0
09-25 18:19:58.127:W / dalvikvm(4982):主题ID = 11:螺纹未捕获的异常退出(组= 0x40d46438)
09-25 18:19:58.147:E / AndroidRuntime(4982):致命异常:螺纹156822
09-25 18:19:58.147:E / AndroidRuntime(4982):显示java.lang.NullPointerException
09-25 18:19:58.147:E / AndroidRuntime(4982):在com.example.practicesurface.MySurfaceView.onDraw(MySurfaceView.java:129)
09-25 18:19:58.147:E / AndroidRuntime(4982):在com.example.practicesurface.MySurfaceView $ MySurfaceThread.run(MySurfaceView.java:39)


解决方案

移动此行code的:

 线程=新MySurfaceThread(getHolder(),这一点);

从MySurfaceView的init()方法,以surfaceCreated()方法。

在移动方向,的onCreate方法被召回,但MySurfaceView的构造不会被调用,因为它被保留。

希望这有助于:)

I was reading this tutorial on how to use a custom surface view class in an XML layout and when I ran the code, the app crashed when my phone's orientation changed. I've noticed a lot of examples involving custom threads and surface view subclasses crashing when the orientation changes, does anybody have any idea why this is happening?

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)

解决方案

move this line of code:

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

from the init() method in MySurfaceView, to the surfaceCreated() method.

When shifting orientation, the onCreate methods are recalled, but the constructor of MySurfaceView won't be called, since it is retained.

Hope this helps :)

这篇关于安卓定制表面观崩溃后方向改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆