You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从菜单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 Android Intent class. This causes compilation confusion or runtime errors.
  • Potential missing manifest registration: If MainActivity isn't declared in your AndroidManifest.xml, the system won't recognize it when you try to start it.

Fixes:

  1. Rename the Intent variable to something like gameIntent to avoid naming conflicts.
  2. Double-check your AndroidManifest.xml to ensure MainActivity is registered. It should include this entry:
<activity android:name=".MainActivity" />
  1. Verify your Button's ID in activity_menue.xml exactly matches R.id.button—a typo here would make findViewById return 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 context field from your GLSurfaceView entirely. The GLSurfaceView class already provides a getContext() method you can use whenever you need access to the Context.
  • If you need to pass the Context to your Renderer, use a WeakReference to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:20:52