【问题标题】:Why doesn't this code work to set up basic Android OpenGL?为什么这段代码不能设置基本的 Android OpenGL?
【发布时间】:2011-10-01 22:31:28
【问题描述】:

嘿,我正在尝试为一些基本图形设置基本结构。但是,当我运行此代码时,应用程序会让我强制退出模拟器。我使用的是 Android 2.3。

我用这个网站走到了这一步http://developer.android.com/resources/tutorials/opengl/opengl-es10.html

请帮忙。我熟悉 OpenGL,但不适用于 Android

public class SampleActivity extends Activity {
/** Called when the activity is first created. */
private GLSurfaceView mGLView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGLView = new GLSurfaceView(this);     
    setContentView(mGLView);
}

@Override protected void onPause()
{
    super.onPause();
    mGLView.onPause();
}

@Override protected void onResume()
{
    super.onResume();
    mGLView.onResume();

}

   }

class SampleSurfaceView extends GLSurfaceView
{

public Sample2SurfaceView(Context context) {
    super(context);

    setRenderer(new SampleRenderer());
}

}

public class SampleRenderer implements GLSurfaceView.Renderer 
{
private FloatBuffer triangleVB;
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{    
    gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
}       
public void onDrawFrame(GL10 gl)
{        // Redraw background color    
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

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

10-03 00:25:57.561:错误/AndroidRuntime(330):致命异常:主要 10-03 00:25:57.561: 错误/AndroidRuntime(330): java.lang.RuntimeException: 无法恢复活动 {android.SampleActivity}: java.lang.NullPointerException 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.os.Handler.dispatchMessage(Handler.java:99) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.os.Looper.loop(Looper.java:123) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 java.lang.reflect.Method.invoke(Method.java:507) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-03 00:25:57.561:错误/AndroidRuntime(330):在 dalvik.system.NativeStart.main(本机方法) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 由: java.lang.NullPointerException 引起 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.opengl.GLSurfaceView.onResume(GLSurfaceView.java:512) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.TagToMobileAlbum.TagToMobileAlbumActivity.onResume(TagToMobileAlbumActivity.java:28) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.Activity.performResume(Activity.java:3832) 10-03 00:25:57.561: 错误/AndroidRuntime(330): 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 10-03 00:25:57.561: 错误/AndroidRuntime(330): ... 还有 12 个 10-03 00:34:05.661:错误/AndroidRuntime(340):致命异常:主要 10-03 00:34:05.661: 错误/AndroidRuntime(340): java.lang.RuntimeException: 无法恢复活动 {android.TagToMobileAlbum/android.TagToMobileAlbum.TagToMobileAlbumActivity}: java.lang.NullPointerException 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.os.Handler.dispatchMessage(Handler.java:99) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.os.Looper.loop(Looper.java:123) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 java.lang.reflect.Method.invoke(Method.java:507) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-03 00:34:05.661:错误/AndroidRuntime(340):在 dalvik.system.NativeStart.main(本机方法) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 由: java.lang.NullPointerException 引起 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.opengl.GLSurfaceView.onResume(GLSurfaceView.java:512) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.SampleActivity.onResume( SampleActivity.java:28) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.Activity.performResume(Activity.java:3832) 10-03 00:34:05.661: 错误/AndroidRuntime(340): 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 10-03 00:34:05.661: 错误/AndroidRuntime(340): ... 12 更多

【问题讨论】:

  • 当应用强制关闭时,请发布您在 Logcat 中收到的堆栈跟踪。

标签: android opengl-es android-emulator


【解决方案1】:

我遇到了同样的问题,通过查看其他示例代码,我设法解决了它。我不确定为什么会有所不同,但我将对 setRenderer 的调用移到 Activity 构造函数中,这似乎已经解决了问题。所以你的 onCreate 会是这样的:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGLView = new GLSurfaceView(this);
    mGLView.setRenderer(new SampleRenderer())
    setContentView(mGLView);
}

【讨论】:

    【解决方案2】:

    1) SampleSurfaceView 的构造函数与类名不匹配。

    2) 在设置ContentView之前,您需要设置SurfaceViewRenderer

    除其他外,您似乎从未使用过SampleSurfaceView。如果你想扩展GLSurfaceView,你需要使用那个扩展类。将您的私人GLSurfaceView 及其所有实例切换为SampleSurfaceView。目前您的 SampleSurfaceView 类从未使用过。

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多