Android Andengine LiveWallpaper中的空指针异常
我正在使用AnEngine Live墙纸扩展制作LiveWallper。问题是,当我试图在onCreateSecene方法中附加一个精灵(背景图像)时,我得到一个空指针异常。有人能帮我吗 以下是我的密码:Android Andengine LiveWallpaper中的空指针异常,android,nullpointerexception,andengine,live-wallpaper,Android,Nullpointerexception,Andengine,Live Wallpaper,我正在使用AnEngine Live墙纸扩展制作LiveWallper。问题是,当我试图在onCreateSecene方法中附加一个精灵(背景图像)时,我得到一个空指针异常。有人能帮我吗 以下是我的密码: @Override public EngineOptions onCreateEngineOptions() { this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); EngineOptions engin
@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)行上,我得到一个空指针。堆栈跟踪已附加
我找到了另一种解决方案,在墙纸上添加精灵背景 使用SpriteBackground类:-
mMainScene.setBackground(new SpriteBackground(new Sprite(0, 0, backTR, this.getVertexBufferObjectManager())));
这解决了我之前得到的空指针。+1用于自己解决此问题。关键是在onPopulateScene中附加精灵,因为在调用OnCreateCallback之前,其他AndEngine方法无法使用mMainScene。