【发布时间】:2012-11-09 02:56:02
【问题描述】:
我是 Android 上的 OpenGL-ES 的新手,请原谅我提出我的愚蠢问题。我正在为 Android v2.2 - SDK#8 构建这个程序。我的平板电脑最高支持 Android v3.1
我正在尝试按照 developer.android.com 上的教程为 Android 设置 OpenGL-ES 环境。该程序编译良好,它应该在设备上显示一个简单的蓝屏。但是,当我尝试在我的 Android 设备上运行它时,我得到了“IllegalStateException: setRenderer has already been called for this instance”错误。
下面是我的代码:
public class TA_SpaceActivity extends Activity
{
private MyGLSurfaceView myGLView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myGLView = new MyGLSurfaceView(this); //NOTE: this is where the app crashed
setContentView(myGLView);
}
}
class MyGLSurfaceView extends GLSurfaceView
{
public MyGLSurfaceView(Context context)
{
super(context);
setRenderer (new MyRenderer());
setEGLContextClientVersion(2);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
}
class MyRenderer implements GLSurfaceView.Renderer
{
public void onSurfaceCreated(GL10 unsued, EGLConfig config)
{
GLES20.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
public void onDrawFrame(GL10 unused)
{
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
public void onSurfaceChanged(GL10 unused, int width, int height)
{
GLES20.glViewport(0, 0, width, height);
}
}
首先,我确保 OpenGLES 的 uses-feature 标签包含在 AndroidManifest.xml 文件中:
然后,当我进行调试运行时,ActivityThread.perfo 显示“未找到源”错误消息。所以,我添加了它的路径(并且我还确保目录中存在 android.jar 文件)
不过,应用程序在“myGLView = new MyGLSurfaceView(this)”行处崩溃了。当我检查 LogCat 时,它显示程序在 setRenderer() 函数调用中抛出了 IllegalStateException。
所以,我目前有 2 个谜题我不明白:
1)为什么在项目中明确定义了指向源的链接时会抛出“未找到源”的错误消息?
2) 为什么说“setRenderer() 已为此实例调用”?我只在我的“MyGLSurfaceView”子类中调用过一次。
对于第一个难题,据我所知,Eclipse 几乎总是会在您犯的每个随机错误时抛出“Source Not Found”消息。这样对吗? (如果没有,请纠正我)。
如果是这种情况,那么我认为问题的根本原因与我的子类中的 setRenderer() 方法有关。经过一整天的混乱,我找不到解决此问题的方法。有没有人给我一些指示我可以尝试解决这个“IllegalStateException: setRenderer() has been called for this instance”的问题?
提前感谢您的帮助。
【问题讨论】:
-
关于“找不到源”。看这个话题:stackoverflow.com/questions/14942851/…
-
关于“找不到源”错误消息。看这个话题:stackoverflow.com/questions/14942851/…