首页 文章

试图在设备上运行我的第一个Andengine应用程序

提问于
浏览
0
I tried Little Child's suggestion, but got "Unfortunately V3 has stopped" on the physical device.                       

Here's some of my code:

package org.andengine.ui.activity; import org.andengine.BuildConfig; import org.andengine.audio.music.MusicManager; import org.andengine.audio.sound.SoundManager; import org.andengine.engine.Engine; import org.andengine.engine.options.EngineOptions; import org.andengine.engine.options.ScreenOrientation; import org.andengine.engine.options.WakeLockOptions; import org.andengine.entity.scene.Scene; import org.andengine.input.sensor.acceleration.AccelerationSensorOptions; import org.andengine.input.sensor.acceleration.IAccelerationListener; import org.andengine.input.sensor.location.ILocationListener; import org.andengine.input.sensor.location.LocationSensorOptions; import org.andengine.input.sensor.orientation.IOrientationListener; import org.andengine.input.sensor.orientation.OrientationSensorOptions; import org.andengine.opengl.font.FontManager; import org.andengine.opengl.shader.ShaderProgramManager; import org.andengine.opengl.texture.TextureManager; import org.andengine.opengl.util.GLState; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.opengl.view.IRendererListener; import org.andengine.opengl.view.RenderSurfaceView; import org.andengine.ui.IGameInterface; import org.andengine.util.ActivityUtils; import org.andengine.util.Constants; import org.andengine.util.debug.Debug; import org.andengine.util.system.SystemUtils; import android.content.Context; import android.content.pm.ActivityInfo; import android.media.AudioManager; import android.os.Bundle; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.view.Gravity; import android.widget.FrameLayout.LayoutParams; / ** *(c)2010 Nicolas Gramlich *(c)2011 Zynga Inc. * * @author Nicolas Gramlich * @since 11:27:06 - 08.03.2010 * / public abstract class BaseGameActivity extends BaseActivity实现IGameInterface,IRendererListener {/ / ================================================= ========== //常数// =================================== ======================== // ======================== ===================================
//字段
// ================================================ ===========

受保护的发动机mEngine;

私人WakeLock mWakeLock;

protected RenderSurfaceView mRenderSurfaceView;

private boolean mGamePaused;
private boolean mGameCreated;
private boolean mCreateGameCalled;
private boolean mOnReloadResourcesScheduled;

// ================================================ ===========
//构造函数
// ================================================ ===========

@覆盖
protected void onCreate(final Bundle pSavedInstanceState){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onCreate”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

super.onCreate(pSavedInstanceState);

this.mGamePaused = true;

this.mEngine = this.onCreateEngine(this.onCreateEngineOptions());
this.mEngine.startUpdateThread();

this.applyEngineOptions();

this.onSetContentView();
}

@覆盖
public Engine onCreateEngine(final EngineOptions pEngineOptions){
返回新引擎(pEngineOptions);
}

@覆盖
public synchronized void onSurfaceCreated(final GLState pGLState){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onSurfaceCreated”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

if(this.mGameCreated){
this.onReloadResources();

if(this.mGamePaused && this.mGameCreated){
this.onResumeGame();
}
} else {
if(this.mCreateGameCalled)
else {
this.mCreateGameCalled = true;
this.onCreateGame();
}
}
}

@覆盖
public synchronized void onSurfaceChanged(final GLState pGLState,final int pWidth,final int pHeight){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onSurfaceChanged(Width =”pWidth“,Height =”pHeight“)”“@(Thread:'”Thread.currentThread() . getName()“')” );
}
}

protected synchronized void onCreateGame(){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onCreateGame”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

final OnPopulateSceneCallback onPopulateSceneCallback = new OnPopulateSceneCallback(){
@覆盖
public void onPopulateSceneFinished(){
尝试{
if(BuildConfig.DEBUG){
Debug.d(BaseGameActivity.this.getClass() . getSimpleName()“ . onGameCreated”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

BaseGameActivity.this.onGameCreated();
} catch(final Throwable pThrowable){
Debug.e(BaseGameActivity.this.getClass() . getSimpleName()“ . onGameCreated failed . ”“@(Thread:'”Thread.currentThread() . getName()“')”,pThrowable);
}

BaseGameActivity.this.callGameResumedOnUIThread();
}
};

final OnCreateSceneCallback onCreateSceneCallback = new OnCreateSceneCallback(){
@覆盖
public void onCreateSceneFinished(final Scene pScene){
BaseGameActivity.this.mEngine.setScene(pScene);

尝试{
if(BuildConfig.DEBUG){
Debug.d(BaseGameActivity.this.getClass() . getSimpleName()“ . onPopulateScene”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

BaseGameActivity.this.onPopulateScene(pScene,onPopulateSceneCallback);
} catch(final Throwable pThrowable){
Debug.e(BaseGameActivity.this.getClass() . getSimpleName()“ . onPopulateScene failed . ”“@(Thread:'”Thread.currentThread() . getName()“')”,pThrowable);
}
}
};

final OnCreateResourcesCallback onCreateResourcesCallback = new OnCreateResourcesCallback(){
@覆盖
public void onCreateResourcesFinished(){
尝试{
if(BuildConfig.DEBUG){
Debug.d(BaseGameActivity.this.getClass() . getSimpleName()“ . onCreateScene”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

BaseGameActivity.this.onCreateScene(onCreateSceneCallback);
} catch(final Throwable pThrowable){
Debug.e(BaseGameActivity.this.getClass() . getSimpleName()“ . onCreateScene failed . ”“@(Thread:'”Thread.currentThread() . getName()“')”,pThrowable);
}
}
};

尝试{
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onCreateResources”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

this.onCreateResources(onCreateResourcesCallback);
} catch(final Throwable pThrowable){
Debug.e(this.getClass() . getSimpleName()“ . onCreateGame failed . ”“@(Thread:'”Thread.currentThread() . getName()“')”,pThrowable);
}
}

@覆盖
public synchronized void onGameCreated(){
this.mGameCreated = true;

/ *由于潜在的异步资源创建,
*表面可能已经无效
*并且可能需要重新加载资源 . * /
if(this.mOnReloadResourcesScheduled){
this.mOnReloadResourcesScheduled = false;
尝试{
this.onReloadResources();
} catch(final Throwable pThrowable){
Debug.e(this.getClass() . getSimpleName()“ . onReloadResources failed . ”“@(Thread:'”Thread.currentThread() . getName()“')”,pThrowable);
}
}
}

@覆盖
protected synchronized void onResume(){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onResume”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

super.onResume();

this.acquireWakeLock();
this.mRenderSurfaceView.onResume();
}

@覆盖
public synchronized void onResumeGame(){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onResumeGame”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

this.mEngine.start();

this.mGamePaused = false;
}

@覆盖
public synchronized void onWindowFocusChanged(final boolean pHasWindowFocus){
super.onWindowFocusChanged(pHasWindowFocus);

if(pHasWindowFocus && this.mGamePaused && this.mGameCreated){
this.onResumeGame();
}
}

@覆盖
public void onReloadResources(){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onReloadResources”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

this.mEngine.onReloadResources();
}

@覆盖
protected void onPause(){
if(BuildConfig.DEBUG){Debug.d(this.getClass() . getSimpleName()“ . onPause”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

super.onPause();

this.mRenderSurfaceView.onPause();
this.releaseWakeLock();

if(!this.mGamePaused){
this.onPauseGame();
}
}

@覆盖
public synchronized void onPauseGame(){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onPauseGame”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

this.mGamePaused = true;

this.mEngine.stop();
}

@覆盖
protected void onDestroy(){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onDestroy”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

super.onDestroy();

this.mEngine.onDestroy();

尝试{
this.onDestroyResources();
} catch(final Throwable pThrowable){
Debug.e(this.getClass() . getSimpleName()“ . onDestroyResources failed . ”“@(Thread:'”Thread.currentThread() . getName()“')”,pThrowable);
}

this.onGameDestroyed();

this.mEngine = null;
}

@覆盖
public void onDestroyResources()抛出异常{
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onDestroyResources”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

if(this.mEngine.getEngineOptions() . getAudioOptions() . needsMusic()){
this.getMusicManager()releaseAll();
}

if(this.mEngine.getEngineOptions() . getAudioOptions() . needsSound()){
this.getSoundManager()releaseAll();
}
}

@覆盖
public synchronized void onGameDestroyed(){
if(BuildConfig.DEBUG){
Debug.d(this.getClass() . getSimpleName()“ . onGameDestroyed”“@(Thread:'”Thread.currentThread() . getName()“')”);
}

this.mGameCreated = false;
}

// ================================================ ===========
// Getter&Setter
// ================================================ ===========

public Engine getEngine(){
返回this.mEngine;
}

public boolean isGamePaused(){
返回this.mGamePaused;
}

public boolean isGameRunning(){
返回!this.mGamePaused;
}

public boolean isGameLoaded(){
返回this.mGameCreated;
}

public VertexBufferObjectManager getVertexBufferObjectManager(){
return this.mEngine.getVertexBufferObjectManager();
}

public TextureManager getTextureManager(){
return this.mEngine.getTextureManager();
}

public FontManager getFontManager(){
return this.mEngine.getFontManager();
}

public ShaderProgramManager getShaderProgramManager(){
return this.mEngine.getShaderProgramManager();
}

public SoundManager getSoundManager(){
return this.mEngine.getSoundManager();
}

public MusicManager getMusicManager(){
return this.mEngine.getMusicManager();
}

// ================================================ ===========
//来自/来自SuperClass / Interfaces的方法
// ================================================ ===========

// ================================================ ===========
// 方法
// ================================================ ===========

private void callGameResumedOnUIThread(){
BaseGameActivity.this.runOnUiThread(new Runnable(){
@覆盖
public void run(){
BaseGameActivity.this.onResumeGame();
}
});
}

protected void onSetContentView(){
this.mRenderSurfaceView = new RenderSurfaceView(this);
this.mRenderSurfaceView.setRenderer(this.mEngine,this);

this.setContentView(this.mRenderSurfaceView,BaseGameActivity.createSurfaceViewLayoutParams());
}

/ **

  • @see引擎#runOnUpdateThread(Runnable接口)
  • /
    public void runOnUpdateThread(final runnable pRunnable){
    this.mEngine.runOnUpdateThread(pRunnable);
    }

/ **

  • @see Engine#runOnUpdateThread(Runnable,boolean)
  • /
    public void runOnUpdateThread(final runnable pRunnable,final boolean pOnlyWhenEngineRunning){
    this.mEngine.runOnUpdateThread(pRunnable,pOnlyWhenEngineRunning);
    }

private void acquireWakeLock(){
this.acquireWakeLock(this.mEngine.getEngineOptions()getWakeLockOptions());
}

private void acquireWakeLock(final WakeLockOptions pWakeLockOptions){
if(pWakeLockOptions == WakeLockOptions.SCREEN_ON){
ActivityUtils.keepScreenOn(本);
} else {
最终PowerManager pm =(PowerManager)this.getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(pWakeLockOptions.getFlag()| PowerManager.ON_AFTER_RELEASE,Constants.DEBUGTAG);
尝试{
this.mWakeLock.acquire();
} catch(final SecurityException pSecurityException){
Debug.e(“你必须添加\ n \ t <uses-permission android:name = \”android.permission.WAKE_LOCK \“/> \ n到你的AndroidManifest.xml!”,pSecurityException);
}
}
}

private void releaseWakeLock(){
if(this.mWakeLock!= null && this.mWakeLock.isHeld()){
this.mWakeLock.release();
}
}

private void applyEngineOptions(){
final EngineOptions engineOptions = this.mEngine.getEngineOptions();

if(engineOptions.isFullscreen()){
ActivityUtils.requestFullscreen(本);
}

if(engineOptions.getAudioOptions() . needsMusic()|| engineOptions.getAudioOptions() . needsSound()){
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

switch(engineOptions.getScreenOrientation()){
案例LANDSCAPE_FIXED:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
打破;
案例LANDSCAPE_SENSOR:
if(SystemUtils.SDK_VERSION_GINGERBREAD_OR_LATER){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else {
此设备不支持Debug.w(ScreenOrientation.class.getSimpleName()“ . ”ScreenOrientation.LANDSCAPE_SENSOR“ . 回退到”ScreenOrientation.class.getSimpleName()“ . ”ScreenOrientation.LANDSCAPE_FIXED“;
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
打破;
案例PORTRAIT_FIXED:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
打破;
案例PORTRAIT_SENSOR:
if(SystemUtils.SDK_VERSION_GINGERBREAD_OR_LATER){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
} else {
此设备不支持Debug.w(ScreenOrientation.class.getSimpleName()“ . ”ScreenOrientation.PORTRAIT_SENSOR“ . 回退到”ScreenOrientation.class.getSimpleName()“ . ”ScreenOrientation.PORTRAIT_FIXED“;
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
打破;
}
}

protected static LayoutParams createSurfaceViewLayoutParams(){
final LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;
return layoutParams;
}

protected void enableVibrator(){
this.mEngine.enableVibrator(本);
}

/ **

  • @see {@link Engine #enableLocationSensor(Context,ILocationListener,LocationSensorOptions)}
  • /
    protected void enableLocationSensor(final ILocationListener pLocationListener,final LocationSensorOptions pLocationSensorOptions){
    this.mEngine.enableLocationSensor(this,pLocationListener,pLocationSensorOptions);
    }

/ **

  • @see {@link Engine #disableLocationSensor(Context)}
  • /
    protected void disableLocationSensor(){
    this.mEngine.disableLocationSensor(本);
    }

/ **

  • @see {@link Engine #enableAccelerationSensor(Context,IAccelerationListener)}
  • /
    protected boolean enableAccelerationSensor(final IAccelerationListener pAccelerationListener){
    返回this.mEngine.enableAccelerationSensor(这个,pAccelerationListener);
    }

/ **

  • @see {@link Engine #enableAccelerationSensor(Context,IAccelerationListener,AccelerationSensorOptions)}
  • /
    protected boolean enableAccelerationSensor(final IAccelerationListener pAccelerationListener,final AccelerationSensorOptions pAccelerationSensorOptions){
    return this.mEngine.enableAccelerationSensor(this,pAccelerationListener,pAccelerationSensorOptions);
    }

/ **

  • @see {@link Engine #disableAccelerationSensor(Context)}
  • /
    protected boolean disableAccelerationSensor(){
    return this.mEngine.disableAccelerationSensor(this);
    }

/ **

  • @see {@link Engine #enableOrientationSensor(Context,IOrientationListener)}
  • /
    protected boolean enableOrientationSensor(final IOrientationListener pOrientationListener){
    return this.mEngine.enableOrientationSensor(this,pOrientationListener);
    }

/ **

  • @see {@link Engine #enableOrientationSensor(Context,IOrientationListener,OrientationSensorOptions)}
  • /
    protected boolean enableOrientationSensor(final IOrientationListener pOrientationListener,final OrientationSensorOptions pLocationSensorOptions){
    return this.mEngine.enableOrientationSensor(this,pOrientationListener,pLocationSensorOptions);
    }

/ **

  • @see {@link Engine#disableOrientationSensor(Context)}
  • /
    protected boolean disableOrientationSensor(){
    return this.mEngine.disableOrientationSensor(this);
    }

// ================================================ ===========
//内部和匿名类
// ================================================ ===========
*********************************** StartActivity ************* ****包com.pearson.lagp.v3; import org.andengine.engine.Engine; import org.andengine.engine.LimitedFPSEngine; import org.andengine.engine.camera.Camera; import org.andengine.engine.options.EngineOptions; import org.andengine.engine.options.ScreenOrientation; import org.andengine.engine.options.resolutionPolicy.RatioResolutionPolicy; import org.andengine.entity.scene.Scene; import org.andengine.entity.sprite.Sprite; import org.andengine.entity.util.FPSLogger; import org.andengine.opengl.texture.Texture; import org.andengine.opengl.texture.TextureOptions; import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas; import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory; import org.andengine.opengl.texture.region.TextureRegion; import org.andengine.opengl.texture.region.TextureRegionFactory; import org.andengine.ui.activity.BaseGameActivity;公共类StartActivity扩展了BaseGameActivity {// ========================================== ================= //常量// ============================ =============================== private static final int CAMERA_WIDTH = 480; private static final int CAMERA_HEIGHT = 320; //让我们的脚湿透:启动画面29 // ======================================= ====================== //字段// ======================= ====================================私人相机mCamera; private BitmapTextureAtlas mTexture; private TextureRegion mSplashTextureRegion; // ================================================ =========== //来自SuperClass / Interfaces的方法// ============================ =============================== public Engine onLoadEngine(){this.mCamera = new Camera(0,0,CAMERA_WIDTH, CAMERA_HEIGHT);返回new Engine(new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),this.mCamera)); public void onLoadResources(){this.mTexture = new BitmapTextureAtlas(null,512,512,TextureOptions.BILINEAR_PREMULTIPLYALPHA); //this.mSplashTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture,this,“gfx / Untitled.png”,0,0);

this.mSplashTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTexture,this,“gfx / Untitled.png”,0,0);
. this.mEngine.getTextureManager()loadTexture(this.mTexture); public Scene onLoadScene(){this.mEngine.registerUpdateHandler(new FPSLogger()); final场景=新场景(1); / *将闪光灯置于相机中心 . * / final int centerX =(int)((CAMERA_WIDTH - this.mSplashTextureRegion.getWidth())/ 2); final int centerY =(int)((CAMERA_HEIGHT -this.mSplashTextureRegion.getHeight())/ 2); / *创建精灵并将其添加到场景中 . * /
// final Sprite splash = new Sprite(centerX,centerY,this.mSplashTextureRegion);
final sprite splash = new Sprite(centerX,centerY,this.mSplashTextureRegion,mEngine.getVertexBufferObjectManager());

scene.getLastChild()attachChild(飞溅) .
//第2章游戏30元素和工具
回归场景;
public void onLoadComplete(){} @Override public EngineOptions onCreateEngineOptions(){// TODO自动生成的方法存根返回null; } @Override public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback)throws Exception {// TODO Auto-generated method stub} @Override public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)throws Exception {// TODO Auto-generated method stub} @Override public void onPopulateScene(Scene pScene,OnPopulateSceneCallback pOnPopulateSceneCallback)抛出异常{// TODO自动生成的方法存根} @Override public Engine onCreateEngine(EngineOptions pEngineOptions){return new LimitedFPSEngine(pEngineOptions,60); ******************* AndroidManifest **************************** * <?xml version =“1.0”encoding =“utf-8”?>
package =“com.pearson.lagp.v3”android:versionCode =“1”android:versionName =“1.0”> <uses-sdk
机器人:的minSdkVersion = “8”
android:targetSdkVersion =“14”/>

<应用
机器人:allowBackup = “真”
机器人:图标= “@绘制/ ic_launcher”>
<活动
机器人:名字= “com.pearson.lagp.v3.StartActivity”
android:label =“@ string / app_name”>
<意图滤波器>
<action android:name =“android.intent.action.MAIN”/>

<category android:name =“android.intent.category.LAUNCHER”/>
</意图滤波器>
</活动>
</应用>
`

1 回答

  • 0

    它非常清楚地表明,在 BaseGameActivity 类的代码的第80行 ActivityonCreate() 方法中有一个 NullPointerException . 了解更多 :)

    BaseGameActivity.onCreate(BaseGameActivity.java:80)
    

    whenWillPeopleLearnToReadStackTraces?

相关问题