gpt4 book ai didi

android - GLSurfaceView 生命周期方法 onPause() 和 onResume()

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:11 24 4
gpt4 key购买 nike

我试图在我的应用程序中正确使用 OpenGL,但在使用生命周期方法时遇到了一些问题:onPause()onResume() .

documentation状态:

Activity Life-cycle

A GLSurfaceView must be notified when the activity is paused and resumed. GLSurfaceView clients are required to call onPause() when the activity pauses and onResume() when the activity resumes. These calls allow GLSurfaceView to pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate the OpenGL display.

首先,我有点困惑,因为 onPause()onResume() 在 Android 的其余部分中都是系统调用的生命周期事件(Activity/fragment )。这里,从描述(包括onPause()onResume()中的详细描述)来看,他们似乎必须由开发者手动调用。

我一直在使用 GLSurfaceView 没有调用 onPause()onResume() 并且没有注意到一个问题。但是,为了玩球(并减少恢复 Activity 时奇怪行为的可能嫌疑人),我正在尝试按照文档中的描述添加它们。

不过似乎使用 onResume() 会导致抛出 NullPointerException:

07-23 13:20:08.570: E/AndroidRuntime(20777): Caused by: java.lang.NullPointerException
07-23 13:20:08.570: E/AndroidRuntime(20777): at android.opengl.GLSurfaceView.onResume(GLSurfaceView.java:544)
07-23 13:20:08.570: E/AndroidRuntime(20777): at com.ActivityNameHere.onResume(ActivityNameHere.java:151)

我是否缺少将 Activity 注册到 View 的步骤?

最佳答案

onPause()onResume() 仍在破坏我的实现 - 但公平地说,它们不再发生崩溃:

Documentation:

The following GLSurfaceView methods can only be called after setRenderer is called:

getRenderMode()
onPause()
onResume()
queueEvent(Runnable)
requestRender()
setRenderMode(int)

在某些情况下,我没有Activity.onResume() 之前调用 setRenderer()

关于android - GLSurfaceView 生命周期方法 onPause() 和 onResume(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com