Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我的身体返回空值?_Android_Nullpointerexception_Andengine - Fatal编程技术网

Android 为什么我的身体返回空值?

Android 为什么我的身体返回空值?,android,nullpointerexception,andengine,Android,Nullpointerexception,Andengine,我正在使用AndEngine为Android平台制作一个游戏。我需要检测一个圆形物体是否接触到一个圆形精灵。要做到这一点,我要计算两者的中心和半径,并使用毕达格来确定它们是否确实接触。然而,当我试图得到我身体的x坐标时,我总是得到一个nullpointerexception。每次触摸屏幕并创建精灵时,精灵数量都会增加。你知道为什么吗?我的代码: public class grow implements Runnable{ Sprite sp; Body body[] = new

我正在使用AndEngine为Android平台制作一个游戏。我需要检测一个圆形物体是否接触到一个圆形精灵。要做到这一点,我要计算两者的中心和半径,并使用毕达格来确定它们是否确实接触。然而,当我试图得到我身体的x坐标时,我总是得到一个nullpointerexception。每次触摸屏幕并创建精灵时,精灵数量都会增加。你知道为什么吗?我的代码:

public class grow implements Runnable{
    Sprite sp;
    Body body[] = new Body[100];
    public grow(Sprite sprite){
        sp = sprite;
    }
    @Override
    public void run() {
        float radf, rads; //fill radius/stationary radius
        float[] fill; //fillx/stationaryx
        float fx=0, fy=0, sx, sy;

        while(down){
            //Log.e("Tag","Running thread. Down = "+Boolean.toString(down));
             yourSprite[spriteNum].setScale(scale += 0.1);
             fill = yourSprite[spriteNum].getSceneCenterCoordinates();
             fill[0]=fx;
             fill[1]=fy;
             radf=yourSprite[spriteNum].getHeightScaled()/2;    

             Log.e("Fill X before for",Float.toString(fx));

            if(spriteNum>0)
                for(int x=0;x<spriteNum;x++){               
                    rads=yourSprite[x].getHeightScaled()/2;
                    sx = body[x].getWorldCenter().x; //Null pointer exception
                    sy = body[x].getWorldCenter().y;

                    if(Math.sqrt(Math.pow((fx-sx),2)+Math.pow((fy-sy),2))<(radf+rads))
                            down = false;
                    Log.e("Fill x",Float.toString(fx));
                    Log.e("Stat x",Float.toString(sy));
                }
             try {
                Thread.sleep(75);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        body[spriteNum] = PhysicsFactory.createCircleBody(mPhysicsWorld, yourSprite[spriteNum], BodyType.DynamicBody, FIXTURE_DEF);
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(yourSprite[spriteNum], body[spriteNum], true, true));
        if(body[0]!=null)
            Log.e("Body created","not null"); //This says it's not null, but when I did the same thing just inside of my for() loop and it did say it was null
    }
public类grow实现可运行{
雪碧;
正文[]=新正文[100];
公共种植(雪碧雪碧){
sp=雪碧;
}
@凌驾
公开募捐{
浮动半径,rads;//填充半径/固定半径
float[]fill;//fillx/stationaryx
浮点数fx=0,fy=0,sx,sy;
当(向下){
//Log.e(“Tag”、“Running thread.Down=“+Boolean.toString(Down));
你的精灵[spriteNum].setScale(scale+=0.1);
fill=yourSprite[spriteNum].GetSceneCenter坐标();
填充[0]=fx;
填充[1]=财年;
radf=yourSprite[spriteNum].getHeightScaled()/2;
Log.e(“在前填充X”,Float.toString(fx));
如果(spriteNum>0)

对于(int x=0;x我想这是因为您初始化了body数组
body body[]=new body[100]
而不是其中的单个
body
元素。因此,当您访问
body[x]
时,它返回null。您还应该初始化数组中的body元素。类似于:

for(int i=0; i<body.length; i++) {
   // probably some cod here..
   body[i] = new Body(..);
   // probably some cod here..
}

for(int i=0;i我想这是因为您已经初始化了body数组
body body[]=new body[100]
而不是其中的单个
body
元素。因此,当您访问
body[x]
时,它返回null。您还应该初始化数组中的body元素。类似于:

for(int i=0; i<body.length; i++) {
   // probably some cod here..
   body[i] = new Body(..);
   // probably some cod here..
}
for(int i=0;i