创建声音时出现AndEngine NullpointerException

时间:2012-08-02 18:04:23

标签: android nullpointerexception andengine audio

快速提问: 我有这个代码:

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

我最后在另一个将这项活动作为一个领域的课上播放:

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

任何想法为什么我会得到一个nullpointer异常?

谢谢!

1 个答案:

答案 0 :(得分:1)

对于您正在使用的播放声音

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

这看起来像是问题,因为此处活动对象未在Zancudo类中实例化。

要访问testSound,您需要获取 Zancudo 活动的实例。