Android - 从渲染器切换活动
在我的 Android 应用程序中,我想从渲染器切换活动。当我创建渲染器时,我在构造函数中传递上下文。在我的渲染器中的 onDrawFrame 函数中:
public MyRenderer(Context ctx){
this.context=ctx;
}
public void onDrawFrame(GL10 gl) {
testFlag = renderFrame();
if(testFlag > 0)
{
Intent myIntent = new Intent(this.context, MyActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.context.startActivity(myIntent);
testFlag = 0;
return;
}
}
这会在我的主要活动中调用 onPause() 来处理一些 OpenGL 人员。
切换活动时出现错误:
此时我收到以下错误:
在没有当前上下文的情况下调用 OpenGL ES API(每个线程记录一次)
有人可以帮助我吗?我意识到这是因为对 OpenGL 的调用不是从 OpenGL 线程进行的,但我该如何修复它?
从渲染器内切换活动的正确方法是什么?
In my Android app I want to switch activities from my renderer. When I create the Renderer I pass context in the constructor. In my Renderer in the onDrawFrame function:
public MyRenderer(Context ctx){
this.context=ctx;
}
public void onDrawFrame(GL10 gl) {
testFlag = renderFrame();
if(testFlag > 0)
{
Intent myIntent = new Intent(this.context, MyActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.context.startActivity(myIntent);
testFlag = 0;
return;
}
}
This calls the onPause() in my main activity which handles some OpenGL staff..
What I get is an error when switching the acivities:
At that point I receive the following error:
call to OpenGL ES API with no current context ( logged once per thread)
Can anyone please help me? I realize that this is caused because a call to OpenGL is not made from the OpenGL thread but how do I fix it??
What is the proper way to switch activities from within the renderer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用了这些来源:
如何从非活动启动活动活动类?
如何重新启动 Android Activity
这是在非活动类(例如渲染器)的类内切换活动的解决方案
I used those sources:
How can I start an Activity from a non-Activity class?
How do I restart an Android Activity
This is solution to switch activities inside class that is non-Activity class (for example Renderer)
来自 GLSurfaceView.Renderer 文档:
您应该能够通过从
onPause()
方法发布的此类 Runnable 正确执行 OpenGL 调用。我不认为您从 GL 线程启动活动切换这一事实是问题的一部分(它确实看起来有点奇怪,但我假设您有充分的理由) :))。
From the GLSurfaceView.Renderer docs:
You should be able to do your OpenGL calls correctly through such a Runnable, posted from your
onPause()
method.I don't think the fact that you're initiating the activity switch from the GL thread is part of the issue (it does seem a bit weird, but I will assume you have a good reason for it :)).