Luisjomen2a
Luisjomen2a

Reputation: 251

AndEngine NullpointerException when creating sound

Quick question: I've got this code:

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;

}

And

protected void onCreateResources() {





    SoundFactory.setAssetBasePath("mfx/");



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

And I finaly play it on another class that has this activity as a field :

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

Any ideas why am I getting a nullpointer exception ?

Thank you!

Upvotes: 0

Views: 427

Answers (1)

Dharmendra
Dharmendra

Reputation: 33996

For play sound you are using like below

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

This looks like the issue because here the activity object is not instantiated in the Zancudo class.

To access the testSound you need to get the instance of the Zancudo activity.

Upvotes: 1

Related Questions