2012-08-02 82 views
0

快速的問題: 我有這樣的代碼:AndEngine NullPointerException異常時聲音

public EngineOptions onCreateEngineOptions() { 
    instance = this; 

    mCamera = new org.andengine.engine.camera.Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

    EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera); 
    engineOptions.getAudioOptions().setNeedsSound(true); 
    //engineOptions.getAudioOptions().setNeedsMusic(true); 


    return engineOptions; 

} 

而且

protected void onCreateResources() { 





    SoundFactory.setAssetBasePath("mfx/"); 



    try { 
     this.testSound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "explosion.ogg"); 
    } catch (final IOException e) { 
     Debug.e(e); 
    } 
} 

我finaly發揮它有這個活動作爲一個領域的又一類:

activity.mCurrentScene.registerTouchArea(image); 
    activity.mCurrentScene.setOnAreaTouchListener(new IOnAreaTouchListener() { 

     @Override 
     public boolean onAreaTouched(TouchEvent pSceneTouchEvent, 
       ITouchArea pTouchArea, float pTouchAreaLocalX, 
       float pTouchAreaLocalY) { 
      Zancudo.this.activity.testSound.play(); 
      return false; 
     } 
    }); 

任何想法爲什麼我會得到一個空指針異常?

謝謝!

回答

1

對於播放聲音您使用的是像下面

Zancudo.this.activity.testSound.play(); 

這看起來像這個問題,因爲這裏的活動對象在Zancudo類沒有實例化。

要訪問testSound,您需要獲取Zancudo活動的實例。