gpt4 book ai didi

java - Android onCreate 没有被调用?

转载 作者:太空狗 更新时间:2023-10-29 14:28:29 30 4
gpt4 key购买 nike

问题是 onCreate() 没有被调用!我真的不知道原因是什么,但我知道在 onCreate() 和 onSurfaceCreated() 中没有调用 Log.d()。我正在尝试使用 OpenGL ES 从我正在阅读的名为 Beginning Android Games 2011 的书中制作一个三角形。示例代码在这里:http://beginning-android-games.googlecode.com/svn/trunk/ch07-gl-basics/

这是我的代码:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="another.game"
android:versionCode="1"
android:versionName="1.0" >

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true" >
<activity
android:name=".Main"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8"/>

</manifest>

主.java

package another.game;

public class Main extends GLTriangleGame {

// @Override
// public Screen getStartScreen() {
// return new LoadingScreen(this);
// }
}

GLGame.java

package framework.impl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import framework.Audio;
import framework.FileIO;
import framework.Game;
import framework.Graphics;
import framework.Input;
import framework.Screen;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public abstract class GLGame extends Activity implements Game, Renderer {
enum GLGameState {
Initialized,
Running,
Paused,
Finished,
Idle
}

GLSurfaceView glView;
GLGraphics glGraphics;
Audio audio;
Input input;
FileIO fileIO;
Screen screen;
GLGameState state = GLGameState.Initialized;
Object stateChanged = new Object();
long startTime = System.nanoTime();
WakeLock wakeLock;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.d("requestWindowFeature", "Past point");
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

glView = new GLSurfaceView(this);
glView.setRenderer(this);
setContentView(glView);

glGraphics = new GLGraphics(glView);
fileIO = new AndroidFileIO(getAssets());
audio = new AndroidAudio(this);
input = new AndroidInput(this, glView, 1, 1);
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "AnotherGame");
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.d("onSurfaceCreated()", "Worked!");
glGraphics.setGl(gl);

synchronized(stateChanged) {
if(state == GLGameState.Initialized) {
screen = getStartScreen();
Log.d("getStartScreen()", "Worked!");
}
state = GLGameState.Running;
screen.resume();
startTime = System.nanoTime();
}
}

@Override
public void onDrawFrame(GL10 arg0) {
GLGameState state = null;

synchronized(stateChanged) {
state = this.state;
}

if(state == GLGameState.Running) {
float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
startTime = System.nanoTime();

screen.update(deltaTime);
screen.present(deltaTime);
}

if(state == GLGameState.Paused) {
screen.pause();
synchronized(stateChanged) {
this.state = GLGameState.Idle;
stateChanged.notifyAll();
}
}

if(state == GLGameState.Finished) {
screen.pause();
screen.dispose();
synchronized(stateChanged) {
this.state = GLGameState.Idle;
stateChanged.notifyAll();
}
}
}

@Override
public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
// Not much to do. Screen will note change!
}

@Override
public void setScreen(Screen screen) {
if(screen!= null) {
this.screen.pause();
this.screen.dispose();
screen.resume();
screen.update(0);
this.screen = screen;
} else {
throw new IllegalArgumentException("Screen must not be null!");
}
}

public GLGraphics getGLGraphics() {
return this.glGraphics;
}

public Graphics getGraphics() {
throw new IllegalStateException("Using OpenGL!");
}

@Override
public Input getInput() {
return this.input;
}

@Override
public Screen getScreen() {
return this.screen;
}

@Override
public Audio getAudio() {
return this.audio;
}

@Override
public void onResume() {
super.onResume();
glView.onResume();
wakeLock.acquire();
}

@Override
public void onPause() {
synchronized(stateChanged) {
if(isFinishing()) {
state = GLGameState.Finished;
} else {
state = GLGameState.Paused;
}
while(true) {
try {
stateChanged.wait();
break;
} catch(Exception e) {
}
}
}
wakeLock.release();
glView.onPause();
super.onPause();
}
}

GLTriangleGame.java

package another.game;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.PowerManager.WakeLock;
import framework.Game;
import framework.Screen;
import framework.impl.FastRenderView;
import framework.impl.GLGame;
import framework.impl.GLGraphics;

public class GLTriangleGame extends GLGame {

@Override
public Screen getStartScreen() {
return new TriangleScreen(this);
}

public class TriangleScreen extends Screen {
GLGraphics glGraphics;
FloatBuffer vertices;

public TriangleScreen(Game game) {
super(game);
glGraphics = ((GLGame) game).getGLGraphics();

ByteBuffer byteBuffer = ByteBuffer.allocate(3 * 2 * 4);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
vertices.put(new float[] {
0.0f, 0.0f,
319.0f, 0.0f,
160.0f, 479.0f
});
vertices.flip();
}

@Override
public void update(float deltaTime) {
game.getInput().getTouchEvents();
game.getInput().getKeyEvents();
}

@Override
public void present(float deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, 320, 0, 480, 1, -1);

gl.glColor4f(1, 0, 0, 1);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertices);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}

@Override
public Bitmap getCurrentScreen() {
// Nothing yet
return null;
}

@Override
public void pause() {
// Nothing yet
}

@Override
public void resume() {
// Nothing yet
}

@Override
public void dispose() {
// Nothing yet
}
}

@Override
public Assets getAssetsClass() {
// TODO Auto-generated method stub
return null;
}

@Override
public FastRenderView getFastRenderView() {
// TODO Auto-generated method stub
return null;
}

@Override
public int getFrameWidth() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getFrameHeight() {
// TODO Auto-generated method stub
return 0;
}

@Override
public float getScaleX() {
// TODO Auto-generated method stub
return 0;
}

@Override
public float getScaleY() {
// TODO Auto-generated method stub
return 0;
}

@Override
public Context getContext() {
// TODO Auto-generated method stub
return null;
}

@Override
public WakeLock getWakeLock() {
// TODO Auto-generated method stub
return null;
}

}

GLGraphics.java

package framework.impl;

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

public class GLGraphics {
GLSurfaceView glView;
GL10 gl;

GLGraphics(GLSurfaceView glView) {
this.glView = glView;
}

public GL10 getGL() {
return gl;
}

public void setGl(GL10 gl) {
this.gl = gl;
}

public int getWidth() {
return glView.getWidth();
}

public int getHeight() {
return glView.getHeight();
}
}

最佳答案

您的来源似乎不是您链接到的来源的副本。

在您的源代码中,android:name=".Main"将在包“another.game”中定义的 Main 类中寻找 onCreate。从您的消息来源中不清楚这是否存在。也许您使用的类不应该是抽象的?

关于java - Android onCreate 没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9371868/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com