【问题标题】:Working on Android OpenGL lesson can't figure out what I'm doing wrong在 Android OpenGL 课上工作无法弄清楚我做错了什么
【发布时间】:2013-03-25 20:43:46
【问题描述】:

我正在努力完成这一课
http://developer.android.com/training/graphics/opengl/environment.html
每次我启动应用程序时它都会崩溃,并且 Eclipse 不会告诉我出了什么问题。
这是我的代码

package com.example.opengl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;

public class MyGL20Renderer implements MyGLSurfaceView.Renderer {
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Set the background frame color
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // Redraw Background Color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);

    }



}

下一课

package com.example.opengl;

import android.content.Context;
import android.opengl.GLSurfaceView;

public class MyGLSurfaceView extends GLSurfaceView {

    public MyGLSurfaceView(Context context) {
        super(context);
        // Set the Renderer for drawing on the GLSurfaceVIew
        setRenderer(new MyGL20Renderer());
        // Create an OpenGL ES 2.0 context
        setEGLContextClientVersion(2);
        // Render the view only when there is a change in the drawing data
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

}

最后是 manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.opengl"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.opengl.MyGLSurfaceView" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我一直在尝试google解决这个问题,但是我的google功夫太弱了。

【问题讨论】:

  • 要找出崩溃的原因,请检查“logcat”。我不熟悉 Eclipse,所以我不知道在哪里可以找到它。不过,我确信谷歌会为您提供详细信息。
  • 我找到了 logcat。在 eclispe 中,右上角有三个按钮。调试、Java 和 DDMS。如果您点击调试,它会将您切换到调试视图。但是现在我不能让它再次崩溃,而且从昨晚开始我没有对代码进行任何更改。那好吧。附带说明一下,是否可以像 C++ 一样进行此调试并跳转到它崩溃的行?
  • 谢谢,我会读一读

标签: android opengl-es


【解决方案1】:

您的主要活动是什么?你忘了这个吗?

public class MainActivity extends Activity {

    private GLSurfaceView mGLView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create a GLSurfaceView instance and set it
    // as the ContentView for this Activity.;
    mGLView = new MyGLSurfaceView(this);
    setContentView(mGLView);

    }
}

我也被这个例子困住了,虽然我的程序在之后启动得很好,但它只显示黑屏,而不是声明的内容:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // Set the background frame color
    GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
}

编辑:发现我的错误。我在 MyGL20Renderer 中有错误的导入,它创建了另一个 onSurfaceChanged 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多