Why doesn't this code work to set up basic Android OpenGL?
Hey I am trying to just set up the basic structure for some basic graphics. However, when I run this code the app makes me force quit on the emulator. I am using Android 2.3.
I used this website to get this far http://developer.android.com/resources/tutorials/opengl/opengl-es10.html
Please help. I am familiar with OpenGL just not for 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: ERROR/AndroidRuntime(330): FATAL EXCEPTION: main 10-03 00:25:57.561: ERROR/AndroidRuntime(330): java.lang.RuntimeException: Unable to resume activity {android.SampleActivity}: java.lang.NullPointerException 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): Caused by: java.lang.NullPointerException 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.opengl.GLSurfaceView.onResume(GLSurfaceView.java:512) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.TagToMobileAlbum.TagToMobileAlbumActivity.onResume(TagToMobileAlbumActivity.java:28) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.Activity.performResume(Activity.java:3832) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 10-03 00:25:57.561: ERROR/AndroidRuntime(330): ... 12 more 10-03 00:34:05.661: ERROR/AndroidRuntime(340): FATAL EXCEPTION: main 10-03 00:34:05.661: ERROR/AndroidRuntime(340): java.lang.RuntimeException: Unable to resume activity {android.TagToMobileAlbum/android.TagToMobileAlbum.TagToMobileAlbumActivity}: java.lang.NullPointerException 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.os.Handler.dispatchMessage(Handler.java:99) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.os.Looper.loop(Looper.java:123) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.ActivityThread.main(ActivityThread.java:3683) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at java.lang.reflect.Method.invokeNative(Native Method) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at java.lang.reflect.Method.invoke(Method.java:507) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at com.android.in开发者_C百科ternal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at dalvik.system.NativeStart.main(Native Method) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): Caused by: java.lang.NullPointerException 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.opengl.GLSurfaceView.onResume(GLSurfaceView.java:512) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.SampleActivity.onResume( SampleActivity.java:28) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.Activity.performResume(Activity.java:3832) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 10-03 00:34:05.661: ERROR/AndroidRuntime(340): ... 12 more
I was having the same problem and, from looking at other sample code, managed to solve it. I'm not sure why it makes a difference but I moved the call to setRenderer into the Activity constructor and this seems to have solved the problem. So your onCreate would be something like:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(this);
mGLView.setRenderer(new SampleRenderer())
setContentView(mGLView);
}
1) Your constructor for SampleSurfaceView
does not match the name of the class.
2) You need to set the SurfaceView
's Renderer
before you set the ContentView
.
Among other things, you don't ever seem to be using SampleSurfaceView
. If you want to extend GLSurfaceView
, you need to use that extended class. Switch your private GLSurfaceView
and all instances of it to SampleSurfaceView
. Currently your SampleSurfaceView
class is never used.
精彩评论