3

I am making a livewallpaper using Andengine Live wallpaper extension. The problem is that when i try to attach a sprite (a background image) in OnCreateScene method, I get a null pointer exception. Can someone help me with this?

Following is my code :-

@Override
public EngineOptions onCreateEngineOptions() {

    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    EngineOptions engineOptions = new EngineOptions(false, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
    return engineOptions;

}

@Override
public void onCreateResources(
        OnCreateResourcesCallback rsrCallback)
        throws Exception {

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 
    background = new BitmapTextureAtlas(this.getTextureManager(), 480, 320);
    backTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(background,this.getAssets(), "background_small.png", 0, 0);
    background.load();

    rsrCallback.onCreateResourcesFinished();
}

@Override
public void onCreateScene(OnCreateSceneCallback sceneCallback)
        throws Exception {
    // creating main scene
    mMainScene = new Scene();
    IBackground myBack = new  Background(0.0f, 0.0f, 0.0f);
    mMainScene.setBackground(myBack);

    final Sprite bkground = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, backTR, getVertexBufferObjectManager());
    mMainScene.getLastChild().attachChild(bkground);

    sceneCallback.onCreateSceneFinished(mMainScene);  
}

on line mMainScene.getLastChild().attachChild(bkground), I get a null pointer. The stack trace is attached.

Stack Trace of Error

Swati Rawat
  • 1,729
  • 1
  • 19
  • 34

1 Answers1

2

I found an alternative solution of adding a sprite background to the wallpaper.

Use the SpriteBackground class :-

mMainScene.setBackground(new SpriteBackground(new Sprite(0, 0, backTR, this.getVertexBufferObjectManager())));

This resolves the null pointer that i was getting earlier.

Swati Rawat
  • 1,729
  • 1
  • 19
  • 34
  • 1
    +1 for solving this yourself. The key is do things like attaching sprites in onPopulateScene because mMainScene is not available to other AndEngine methods until after the OnCreateCallback is called. – jmroyalty Sep 20 '12 at 13:59