java.lang.NullPointerException错误帮助我

时间:2014-04-09 13:22:36

标签: java nullpointerexception

这是我的logCat。

04-09 09:11:18.178: E/AndroidRuntime(829): FATAL EXCEPTION: main
04-09 09:11:18.178: E/AndroidRuntime(829): java.lang.NullPointerException
04-09 09:11:18.178: E/AndroidRuntime(829):  at com.example.androidresim.Activitygiris.onLoadScene(Activitygiris.java:84)
04-09 09:11:18.178: E/AndroidRuntime(829):  at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:158)
04-09 09:11:18.178: E/AndroidRuntime(829):  at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:82)
04-09 09:11:18.178: E/AndroidRuntime(829):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2462)
04-09 09:11:18.178: E/AndroidRuntime(829):  at android.view.View.dispatchWindowFocusChanged(View.java:7578)
04-09 09:11:18.178: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:962)
04-09 09:11:18.178: E/AndroidRuntime(829):  at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3115)
04-09 09:11:18.178: E/AndroidRuntime(829):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 09:11:18.178: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:137)
04-09 09:11:18.178: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:5103)
04-09 09:11:18.178: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
04-09 09:11:18.178: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:525)
04-09 09:11:18.178: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-09 09:11:18.178: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-09 09:11:18.178: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)

这是我的班级。

public class Activitygiris extends BaseGameActivity {

    private static final int CAMERA_WIDTH=800;
    private static final int CAMERA_HEIGHT=480;


    private Camera camera;
    private Engine engine;
    Scene sahne;

    private Texture texSaha,texOyuncu1,texOyuncu2,texRedWins,texBlueWins;
    private TextureRegion texRegSaha,texRegOyuncu1,texRegOyuncu2,texRegRedWins,texRegBlueWins;
    private Sprite spriteSaha,spriteOyuncu1,spriteOyuncu2,spriteRedWins,spriteBlueWins;

    private TimerHandler timerBeklet;


    @Override
    public Engine onLoadEngine() {
        // TODO Auto-generated method stub

        camera= new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
        final EngineOptions engineoptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE,new FillResolutionPolicy(),camera);
        engineoptions.getTouchOptions().setRunOnUpdateThread(true);
        engine = new Engine(engineoptions);



        return engine;
    }

    @Override
    public void onLoadResources() {
        // TODO Auto-generated method stub
    texSaha=new Texture(1024,512,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texOyuncu1 = new Texture(128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texOyuncu2 = new Texture(128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    texRedWins = new Texture(64,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);  
    texBlueWins = new Texture(64,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    texRegSaha = TextureRegionFactory.createFromAsset(texSaha, this, "gfx/Arkaplan.jpg",0,0);
    texRegOyuncu1 = TextureRegionFactory.createFromAsset(texOyuncu1, this, "gfx/kol1.png",0,0);
    texRegOyuncu2 = TextureRegionFactory.createFromAsset(texOyuncu2, this, "gfx/kol2.png",0,0); 
    texRegBlueWins = TextureRegionFactory.createFromAsset(texBlueWins, this, "gfx/bluewins.png",0,0);
    texRegRedWins = TextureRegionFactory.createFromAsset(texRedWins, this, "gfx/redwins.png",0,0);

    Texture [] textures = {texSaha,texOyuncu1,texOyuncu2,texBlueWins,texRedWins };  

    mEngine.getTextureManager().loadTextures(textures);
    }
    @Override
    public Scene onLoadScene() {
        // TODO Auto-generated method stub

        this.engine.registerUpdateHandler(new FPSLogger());
        this.sahne=new Scene();

        spriteSaha = new Sprite(0, 0, texRegSaha);
        spriteOyuncu1 = new Sprite(600, CAMERA_HEIGHT/2-64, texRegOyuncu1);
        spriteOyuncu2 = new Sprite(50, CAMERA_HEIGHT/2-64, texRegOyuncu2);
        spriteRedWins = new Sprite(CAMERA_HEIGHT-128,CAMERA_HEIGHT/2-128,texRegRedWins);
        spriteRedWins = new Sprite(CAMERA_HEIGHT+32,CAMERA_HEIGHT/2-128,texRegBlueWins.clone());

        spriteBlueWins.setVisible(false);

        mEngine.registerUpdateHandler(timerBeklet = new TimerHandler(3, false,new ITimerCallback() {

            @Override
            public void onTimePassed(TimerHandler pTimerHandler) {
                // TODO Auto-generated method stub


                spriteRedWins.setRotation(180);
                mEngine.registerUpdateHandler(timerBeklet= new TimerHandler(3,false, new ITimerCallback() {

                    @Override
                    public void onTimePassed(TimerHandler pTimerHandler) {
                        // TODO Auto-generated method stub

                        spriteBlueWins.setVisible(true);
                        spriteRedWins.setVisible(false);

                    }
                }));

            }
        }));

        this.sahne.attachChild(spriteSaha);
        this.sahne.attachChild(spriteOyuncu1);
        this.sahne.attachChild(spriteOyuncu2);
        this.sahne.attachChild(spriteBlueWins);
        this.sahne.attachChild(spriteRedWins);


        return this.sahne;  
    }

    @Override
    public void onLoadComplete() {
        // TODO Auto-generated method stub

    }

} 

请帮帮我..

1 个答案:

答案 0 :(得分:1)

您获得nullPointerException,因为引用变量spriteBlueWin引用了null

您没有初始化spriteBlueWins,您应该像对待其他人一样初始化它。

正如@Dirk Lachowski指出的那样,您可能复制并粘贴了spriteRedWins并忘记将其替换为spriteBlueWins

    spriteSaha = new Sprite(0, 0, texRegSaha);
    spriteOyuncu1 = new Sprite(600, CAMERA_HEIGHT/2-64, texRegOyuncu1);
    spriteOyuncu2 = new Sprite(50, CAMERA_HEIGHT/2-64, texRegOyuncu2);
    spriteRedWins = new Sprite(CAMERA_HEIGHT-128,CAMERA_HEIGHT/2-128,texRegRedWins);
    spriteRedWins = new Sprite(CAMERA_HEIGHT+32,CAMERA_HEIGHT/2-128,texRegBlueWins.clone());

最后一行应该是:

    spriteBlueWins = new Sprite(CAMERA_HEIGHT+32,CAMERA_HEIGHT/2-128,texRegBlueWins.clone());