【问题标题】:Null Pointer Exception in Andengine LiveWallpaperAndengine LiveWallpaper 中的空指针异常
【发布时间】:2012-09-20 06:21:48
【问题描述】:

我正在使用 Andengine 动态壁纸扩展制作动态壁纸。问题是当我尝试在 OnCreateScene 方法中附加一个精灵(背景图像)时,我得到一个空指针异常。有人可以帮我解决这个问题吗?

以下是我的代码:-

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

在线 mMainScene.getLastChild().attachChild(bkground),我得到一个空指针。堆栈跟踪已附加。

【问题讨论】:

    标签: android nullpointerexception andengine live-wallpaper


    【解决方案1】:

    我找到了一种将精灵背景添加到壁纸的替代解决方案。

    使用 SpriteBackground 类:-

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

    这解决了我之前得到的空指针。

    【讨论】:

    • +1 自己解决这个问题。关键是在 onPopulateScene 中附加精灵之类的操作,因为在调用 OnCreateCallback 之前 mMainScene 对其他 AndEngine 方法不可用。
    猜你喜欢
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2015-07-12
    相关资源
    最近更新 更多