Android:尝试启动 OpenGL Activity ,应用程序崩溃

标签 android opengl-es android-activity crash

<分区>

我是 Android 开发的新手。如果这个问题微不足道,请耐心等待。

我有一个包含按钮的主要 Activity :

<Button
     android:id="@+id/single_player"
     style="@style/ButtonTheme"
     android:text="Single Player"
     android:visibility="visible"
     android:onClick="OpenGameActivity" />

以及主 Activity 中的一个方法,用于将按钮路由到:

public void OpenGameActivity()
{
    Intent intent = new Intent(MainActivity.this, GameActivity.class);
    startActivity(intent);
}

现在,GameActivity.class 是一个用于创建 GLSurfaceView 的 Activity :

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;

public class GameActivity extends Activity {
    private GLSurfaceView GridView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        GridView = new GameView(this);
        setContentView(GridView);
    }

GameView 是 GLSurfaceView 的简单实现,它创建 GameRender,GLSurfaceView.Renderer 的简单实现。我根据 http://developer.android.com/training/graphics/opengl/environment.html 上的指南设置了 Activity、SurfaceView 和 Renderer。 渲染器看起来像这样:

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.opengles.GL10;

public class GameRender implements GLSurfaceView.Renderer {


    @Override
    public void onDrawFrame(GL10 unused) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, javax.microedition.khronos.egl.EGLConfig eglConfig) {
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }
}

问题是,每次我从我的 Android 手机上点击应该启动新 Activity 的按钮时,应用程序都会崩溃。我做错了什么?

我在发布这个之前做了我的研究,所以为了澄清我确实将新 Activity 添加到 list 中:

<activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
                android:name=".GameActivity"
                android:parentActivityName=".MainActivity" >
            <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value=".MainActivity" />
    </activity>

最佳答案

尝试改变

public void OpenGameActivity()

public void OpenGameActivity( View View )

如果您从 XML 中引用一个函数,我认为您需要接受一个 View 参数。

关于Android:尝试启动 OpenGL Activity ,应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17016190/

相关文章:

android - 使用 SensorManager 在 OpenGL 中旋转?

android - 在 OpenGL ES 中使用 png/jpg 的正确方法

Java - android - 在后台运行时更改应用程序的 Activity

android - 如何区分是否调用 onDestroy() 作为配置更改序列的一部分?

android - 使用标志 ON_AFTER_RELEASE 释放唤醒锁的问题

android - 如何设置每个 cardview 在 recyclerview 中的位置?

iphone - 程序在gldrawarray崩溃

android - 如何在 facebook android.. 中获得对接受的应用程序请求的响应?

android - 是否可以使用 Flutter 在特定于平台的 Android 服务中运行 dart 代码?

java - 如何在 Firebase 中使子级可扩展并为其添加值?