问题
I'm new to using AndEngine
and am trying to get a sprite to increase in size on touch. It kind of works, but will suddenly get this error sometimes. Any ideas? At this point, I just need enough text so SO will let me post my question. If anyone needs any more information, let me know and I will post it. I'm very open to ideas. If you have a better way or see a problem with my code, please let me know. At this point, the physics world doesn't even work.
06-17 22:44:25.809: E/AndroidRuntime(20526): FATAL EXCEPTION: UpdateThread
06-17 22:44:25.809: E/AndroidRuntime(20526): java.lang.NullPointerException
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.util.modifier.ModifierList.onUpdate(ModifierList.java:66)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.entity.Entity.onManagedUpdate(Entity.java:1392)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.entity.Entity.onUpdate(Entity.java:1167)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.entity.Entity.onManagedUpdate(Entity.java:1402)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:284)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.entity.Entity.onUpdate(Entity.java:1167)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.engine.Engine.onUpdateScene(Engine.java:591)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.engine.Engine.onUpdate(Engine.java:586)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
06-17 22:44:25.809: E/AndroidRuntime(20526): at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)
This is my code:
public class TestGFX2Activity extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener {
/** Called when the activity is first created. */
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion circleTR;
private Scene scene;
boolean down=false;
float scale;
final float growRate = (float) .0001;
private PhysicsWorld mPhysicsWorld;
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
/*Line 66*/ this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 128, TextureOptions.BILINEAR); //Line 66
this.circleTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "circle.png", 0, 0); // 64x32
this.mBitmapTextureAtlas.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.scene = new Scene();
this.scene.setBackground(new Background(0, 1, 0));
Log.e("Tag","Got to background");
this.scene.setOnSceneTouchListener(this);
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
this.scene.attachChild(ground);
this.scene.attachChild(roof);
this.scene.attachChild(left);
this.scene.attachChild(right);
this.scene.registerUpdateHandler(this.mPhysicsWorld);
return this.scene;
}
@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
Sprite face;
Body body;
scale = 1;
if(this.mPhysicsWorld != null) {
if(pSceneTouchEvent.isActionDown()) {
//this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
down = true;
face = new Sprite(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), circleTR, this.getVertexBufferObjectManager());
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
new Thread(new Grow(face)).start();
return true;
} else if(pSceneTouchEvent.isActionUp()){
down = false;
}
}
return false;
}
@Override
public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {
}
@Override
public void onAccelerationChanged(final AccelerationData pAccelerationData) {
final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
@Override
public void onResumeGame() {
super.onResumeGame();
this.enableAccelerationSensor(this);
}
@Override
public void onPauseGame() {
super.onPauseGame();
this.disableAccelerationSensor();
}
private class Grow implements Runnable{
Sprite s;
ScaleModifier mod;
public Grow(Sprite sprite){
s = sprite;
scene.attachChild(s);
}
public void run(){
while(down){
//Log.e("Tag",Float.toString(scale));
s.clearEntityModifiers();
mod = new ScaleModifier((float) .1,scale,scale+growRate);
scale+=growRate;
s.registerEntityModifier(mod);
/*s.detachSelf();
s.setScale(scale);
scale += growRate;
try{
Thread.sleep(100);
}catch(InterruptedException e){}
scene.attachChild(s);*/
}
}
}
}
回答1:
Instead of calling this ina separate Thread, you probably should call this
new Grow(face)).start();
in a runOnUpdate() block as in
myActivity.runonUpdateThread(new Grow(face)));
回答2:
just replace
new Thread(new Grow(face)).start();
with
TestGFX2Activity.runonUpdateThread(new Grow(face)));
来源:https://stackoverflow.com/questions/11077560/why-am-i-getting-an-update-thread-null-pointer-exception-android-andengine