我是Android开发的新手。如果这个问题很简单,请耐心等待。
我有一个包含按钮的主要活动:
<Button
android:id="@+id/single_player"
style="@style/ButtonTheme"
android:text="Single Player"
android:visibility="visible"
android:onClick="OpenGameActivity" />
和主要活动内部的方法,用于路由到的按钮:
public void OpenGameActivity()
{
Intent intent = new Intent(MainActivity.this, GameActivity.class);
startActivity(intent);
}
现在,GameActivity.class是一个用于创建GLSurfaceView的活动:
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
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>
答案 0 :(得分:1)
尝试更改
public void OpenGameActivity()
到
public void OpenGameActivity(View view)
如果您从XML引用函数,我认为您需要接受View参数。
答案 1 :(得分:0)
这里没有Object的实例,像这样更改你的代码
GridView gridview = new GameView(this);
setContentView(gridview );