当前位置: 首页 > 知识库问答 >
问题:

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

邵畅
2023-03-14
I tried Little Child's suggestion, but got "Unfortunately V3 has stopped" on the physical device.                       

Here's some of my code:                     

包org.andengine.ui.activity;

导入org.andEngine.BuildConfig;
导入org.andEngine.Audio.Music.MusicManager;
导入org.andEngine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.Engine.mport org.andengine.opengl.util.glstate;
导入org.andengine.opengl.vbo.VertexBufferObjectManager;
导入org.andengine.opengl.view.iRendererListener;
导入org.andengine.opengl.view.RenderSurfaceView;
导入org.andengine.opengl.view.RenderSurfaceView;
导入org.andengine.ui.iGameInterface;
导入

导入android.content.context;
导入android.content.pm.activityinfo;
导入android.media.audiomanager;
导入android.os.bundle;
导入android.os.PowerManager;
导入android.os.PowerManager.WakeLock;
导入android.View.Gravity;
导入android.Widget.FrameLayout.LayoutParams;

/**
*(c)2010 Nicolas Gramlich
*(c)2011 Zynga Inc.
*
*@作者Nicolas Gramlich
*@自11:27:06-08.03.2010
*/
公共抽象类BaseGameActivity扩展了BaseActivity实现了IGameInterface,IRendererListener{

  // ===========================================================                  
  // Fields                   
  // ===========================================================                  

  protected Engine mEngine;                   

  private WakeLock mWakeLock;                 

  protected RenderSurfaceView mRenderSurfaceView;                 

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

  // ===========================================================                  
  // Constructors                 
  // ===========================================================                  

  @Override                   
  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();                
  }                   

  @Override                   
  public Engine onCreateEngine(final EngineOptions pEngineOptions) {                  
      return new Engine(pEngineOptions);              
  }                   

  @Override                   
  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) {            
              this.mOnReloadResourcesScheduled = true;        
          } else {            
              this.mCreateGameCalled = true;      
              this.onCreateGame();        
          }           
      }               
  }                   

  @Override                   
  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() {             
          @Override           
          public void onPopulateSceneFinished() {         
              try {       
                  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() {               
          @Override           
          public void onCreateSceneFinished(final Scene pScene) {         
              BaseGameActivity.this.mEngine.setScene(pScene);     

              try {       
                  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() {               
          @Override           
          public void onCreateResourcesFinished() {           
              try {       
                  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);    
              }       
          }           
      };              

      try {               
          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);          
      }               
  }                   

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

      /* Since the potential asynchronous resource creation,              
       * the surface might already be invalid             
       * and a resource reloading might be necessary. */              
      if(this.mOnReloadResourcesScheduled) {              
          this.mOnReloadResourcesScheduled = false;           
          try {           
              this.onReloadResources();       
          } catch(final Throwable pThrowable) {           
              Debug.e(this.getClass().getSimpleName() + ".onReloadResources failed." + " @(Thread: '" + Thread.currentThread().getName() + "')", pThrowable);     
          }           
      }               
  }                   

  @Override                   
  protected synchronized void onResume() {                    
      if(BuildConfig.DEBUG) {             
          Debug.d(this.getClass().getSimpleName() + ".onResume" + " @(Thread: '" + Thread.currentThread().getName() + "')");          
      }               

      super.onResume();               

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

  @Override                   
  public synchronized void onResumeGame() {                   
      if(BuildConfig.DEBUG) {             
          Debug.d(this.getClass().getSimpleName() + ".onResumeGame" + " @(Thread: '" + Thread.currentThread().getName() + "')");          
      }               

      this.mEngine.start();               

      this.mGamePaused = false;               
  }                   

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

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

  @Override                   
  public void onReloadResources() {                   
      if(BuildConfig.DEBUG) {             
          Debug.d(this.getClass().getSimpleName() + ".onReloadResources" + " @(Thread: '" + Thread.currentThread().getName() + "')");         
      }               

      this.mEngine.onReloadResources();               
  }                   

  @Override                   
  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();         
      }               
  }                   

  @Override                   
  public synchronized void onPauseGame() {                    
      if(BuildConfig.DEBUG) {             
          Debug.d(this.getClass().getSimpleName() + ".onPauseGame" + " @(Thread: '" + Thread.currentThread().getName() + "')");           
      }               

      this.mGamePaused = true;                

      this.mEngine.stop();                
  }                   

  @Override                   
  protected void onDestroy() {                    
      if(BuildConfig.DEBUG) {             
          Debug.d(this.getClass().getSimpleName() + ".onDestroy" + " @(Thread: '" + Thread.currentThread().getName() + "')");         
      }               

      super.onDestroy();              

      this.mEngine.onDestroy();               

      try {               
          this.onDestroyResources();          
      } catch (final Throwable pThrowable) {              
          Debug.e(this.getClass().getSimpleName() + ".onDestroyResources failed." + " @(Thread: '" + Thread.currentThread().getName() + "')", pThrowable);            
      }               

      this.onGameDestroyed();             

      this.mEngine = null;                
  }                   

  @Override                   
  public void onDestroyResources() throws Exception {                 
      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();            
      }               
  }                   

  @Override                   
  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() {                 
      return this.mEngine;                
  }                   

  public boolean isGamePaused() {                 
      return this.mGamePaused;                
  }                   

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

  public boolean isGameLoaded() {                 
      return 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();              
  }                   

  // ===========================================================                  
  // Methods for/from SuperClass/Interfaces                   
  // ===========================================================                  

  // ===========================================================                  
  // Methods                  
  // ===========================================================                  

  private void callGameResumedOnUIThread() {                  
      BaseGameActivity.this.runOnUiThread(new Runnable() {                
          @Override           
          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 Engine#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(this);           
      } else {                
          final PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);            
          this.mWakeLock = pm.newWakeLock(pWakeLockOptions.getFlag() | PowerManager.ON_AFTER_RELEASE, Constants.DEBUGTAG);            
          try {           
              this.mWakeLock.acquire();       
          } catch (final SecurityException pSecurityException) {          
              Debug.e("You have to add\n\t<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>\nto your 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(this);          
      }               

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

      switch(engineOptions.getScreenOrientation()) {              
          case LANDSCAPE_FIXED:           
              this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);        
              break;      
          case 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 + " is not supported on this device. Falling back to " + ScreenOrientation.class.getSimpleName() + "." + ScreenOrientation.LANDSCAPE_FIXED); 
                  this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    
              }       
              break;      
          case PORTRAIT_FIXED:            
              this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);     
              break;      
          case 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 + " is not supported on this device. Falling back to " + ScreenOrientation.class.getSimpleName() + "." + ScreenOrientation.PORTRAIT_FIXED);   
                  this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
              }       
              break;      
      }               
  }                   

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

  /**                 
   * @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(this);               
  }                   

  /**                 
   * @see {@link Engine#enableAccelerationSensor(Context, IAccelerationListener)}                 
   */                 
  protected boolean enableAccelerationSensor(final IAccelerationListener pAccelerationListener) {                 
      return this.mEngine.enableAccelerationSensor(this, 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);             
  }                   

  // ===========================================================                  
  // Inner and Anonymous Classes                  
  // ===========================================================                  

}

***********活动性;

public class StartActivity extends BaseGameActivity{

public Engine onLoadEngine(){
This.mCamera=新相机(0,0,CAMERA_WIDTH,
CAMERA_HEIGHT);
返回新引擎(新EngineOptions(true,ScreenOrientation.Greandape_Fixed,新RatioResolutionPolicy(CAMERA_WIDTH,
CAMERA_HEIGHT),
This.mCamera));
}

public void onLoadResources(){
This.mTexture=新的BitmapTextureAtlas(null,512,512,TextureOptions.Bilinear_PreMultipleAlpha);

      //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 Scene Scene=新场景(1);
/*在相机上居中溅射。*/
final int Center x=(int)((CAMERA_WIDTH-This.MsplashTextureRegion.GetWidth())/2);
final int Center=(int)((CAMERA_HEIGHT

      /* Create the sprite and add it to the scene. */                
      //final Sprite splash = new Sprite(centerX,centerY, this.mSplashTextureRegion);             
      final Sprite splash = new Sprite(centerX,centerY, this.mSplashTextureRegion,mEngine.getVertexBufferObjectManager());                



      scene.getLastChild().attachChild(splash);               
      //Chapter 2 Game 30 Elements and Tools              
      return scene;               

}

public void onLoadComplete(){
}
@Override
public EngineOptions onCreateEngineOptions(){
//TODO自动生成的方法存根
返回NULL;
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)引发异常{
//TODO自动生成的方法存根

}
@override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
引发异常{
//TODO自动生成的方法存根

}
@override
public void onPopulateScene(场景pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback)引发异常{
//TODO自动生成的方法存根

}

@覆盖公共引擎onCreateEngine(EngineOptions pEngineOptions){返回新的LimitedFPSEngine(pEngineOptions,60);}

*********************************************************

  <?xml version="1.0" encoding="utf-8"?>                      

package=“com.pearson.lagp.v3”
Android:VersionCode=“1”
Android:VersionName=“1.0”>

  <uses-sdk                       
      android:minSdkVersion="8"                       
      android:targetSdkVersion="14" />                        

  <application                        
      android:allowBackup="true"                      
      android:icon="@drawable/ic_launcher">                       
      <activity                       
          android:name="com.pearson.lagp.v3.StartActivity"                        
          android:label="@string/app_name" >                      
          <intent-filter>                     
              <action android:name="android.intent.action.MAIN" />                        

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


`

共有1个答案

松灿
2023-03-14

它非常清楚地表明,在BaseGameActivity类的代码的第80行,您的ActivityonCreate()方法中有一个NullPointerException。研究一下:)

BaseGameActivity.onCreate(BaseGameActivity.java:80)  
 类似资料: