Android onCreate没有被调用?

时间:2012-02-21 03:42:18

标签: java android eclipse opengl-es oncreate

问题是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>

Main.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();
    }
}

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

尝试将实例化的代码放在onResume和onCreate中。

答案 2 :(得分:0)

我认为您在onCreate()方法中缺少以下行。

setContentView(R.layout.main);

答案 3 :(得分:0)

我认为问题在于Log功能。

我是一个Android新手,所以我不确切知道Log功能是如何工作的,但是我没有得到任何反馈就遇到了很大的问题。

我将Log设置为e(error)后,我开始收到反馈。希望它能帮到你。