Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null Pointer Exception in Andengine LiveWallpaper

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

like image 648
Swati Rawat Avatar asked Mar 31 '26 14:03

Swati Rawat


1 Answers

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.

like image 112
Swati Rawat Avatar answered Apr 03 '26 06:04

Swati Rawat