从菜单Activity跳转至含GLSurfaceView的Activity遇问题及返回崩溃求助
Hey Karlj90, let's break down and fix your two issues one by one:
Issue 1: Button onClickListener fails to launch the GL Activity
Looking at your menu Activity code, I spot two critical issues that could block the Activity from launching:
- Variable name collision: You named your Intent variable
Intent, which clashes with the AndroidIntentclass. This causes compilation confusion or runtime errors. - Potential missing manifest registration: If
MainActivityisn't declared in yourAndroidManifest.xml, the system won't recognize it when you try to start it.
Fixes:
- Rename the Intent variable to something like
gameIntentto avoid naming conflicts. - Double-check your
AndroidManifest.xmlto ensureMainActivityis registered. It should include this entry:
<activity android:name=".MainActivity" />
- Verify your Button's ID in
activity_menue.xmlexactly matchesR.id.button—a typo here would makefindViewByIdreturn null, so the click listener never gets attached.
Here's the corrected menu Activity code:
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menue); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent gameIntent = new Intent(Menue.this, MainActivity.class); startActivity(gameIntent); } }); }
Issue 2: Crash when returning to menu due to Context usage
The root cause here is how you're storing the Context in your GLSurfaceView. If GLSurfaceView.context=this uses a static field, it creates a memory leak—the static reference holds onto the Activity even after it's destroyed. When you return to the menu, this stale, invalid Context triggers crashes when you try to use it.
Fixes:
- Remove the custom
contextfield from your GLSurfaceView entirely. The GLSurfaceView class already provides agetContext()method you can use whenever you need access to the Context. - If you need to pass the Context to your Renderer, use a
WeakReferenceto avoid holding a strong reference to the Activity, which prevents leaks.
Corrected GLSurfaceView and Activity code:
MainActivity (GL Activity):
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GLSurfaceView glSurfaceView = new GLSurfaceView(this); setContentView(glSurfaceView); // No need to manually set a context field here }
GLSurfaceView class:
public class MyGLSurfaceView extends GLSurfaceView { private final MyRenderer renderer; public MyGLSurfaceView(Context context) { super(context); renderer = new MyRenderer(new WeakReference<>(context)); // Use WeakReference if context is needed in Renderer setRenderer(renderer); setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); } // Example Renderer with safe Context access private static class MyRenderer implements GLSurfaceView.Renderer { private WeakReference<Context> contextRef; public MyRenderer(WeakReference<Context> contextRef) { this.contextRef = contextRef; } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Access context safely (check if it's still valid) Context context = contextRef.get(); if (context != null) { // Do work with context, e.g., load resources } } @Override public void onSurfaceChanged(GL10 gl, int width, int height) {} @Override public void onDrawFrame(GL10 gl) {} } }
内容的提问来源于stack exchange,提问作者Karlj90
相关产品推荐
相关产品推荐

