質問

My app has a second Activity which contains a GLSurfaceView. This shows an OpenGL ES 1.0 chart of data collated in the first Activity. Everything, including orientation changes, works fine. The app can pop in and out of the two Activities, handle orientation changes and nothing goes wrong.

However, if I am showing the OpenGL chart on the second Activity and press the Android HOME screen, then go back into the app by selecting it from the list of apps on the MENU, I get a message saying "Unfortunately, XXX has stopped."

In Logcat I see this: IllegalStateException: setRenderer has already been called for this instance and I have tracked it down to the mGLView.setRenderer (new GLRenderer (this)); line of code I have in the Activity's onStart() method.

I put log lines immediately in front of and after this call... when the crash happens, I see the following in Logcat...

02-18 09:48:08.743    1953-1953/com.hippo.happ I/HActivity? Just before the setRenderer call...
02-18 09:48:08.753    1953-1953/com.hippo.happ D/AndroidRuntime? Shutting down VM
02-18 09:48:08.753    1953-1953/com.hippo.happ W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0xb0d75b08)
02-18 09:48:08.763    1953-1953/com.hippo.happ E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.hippo.happ, PID: 1953
    java.lang.RuntimeException: Unable to resume activity {com.hippo.happ/com.hippo.happ.HChartActivity}: java.lang.IllegalStateException: setRenderer has already been called for this instance.

But I do not see this, unless the Activity is being created for the first time:

02-18 09:47:51.123    1953-1953/com.hippo.happ I/HActivity? Just after the setRenderer call...

I can confirm that I am not calling setEGLContextClientVersion at all, nor am I altering the rendering mode with setRenderMode. I have implemented onPause() and onResume().

Can I use some code to check whether "setRenderer has already been called for this instance" (I cannot see an appropriate get or check method in the docs) or can I just surround the setRender line in a try..catch block and smother IllegalStateException? Would that be the recommended way to get around this?

役に立ちましたか?

解決

onStart() is called at the beginning of the Activity's visible lifetime. So when you go to the Home screen and return, it is the first lifecyle event that runs. Since you set the renderer in that method, if fails because it was previously set when the Activity was created. You should ideally set the renderer as part of onCreate() since that won't run again when the Activity resumes.

I assume you have a class, that subclasses GLSurfaceView, which you instantiate in onCreate(). You can set the renderer in that class's constructor. Or if your code already does that but instantiates the GlSurfaceView in onStart(), it should hopefully just be a case of moving it to onCreate().

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top