Java 为什么字节[]数组返回的是零而不是位置?

Java 为什么字节[]数组返回的是零而不是位置?,java,android,Java,Android,我在上一篇文章中犯了一些愚蠢的错误,但我回来了,我觉得我有一个更好的问题 我正在调用一个名为getData的函数,它是一个byte[]返回类型,我正在通过数据包将它发送到另一个程序。在程序端,我收到98 0 0 101,它告诉我bDrive.getX和bDrive.getY函数有问题 这些函数是从我创建的一个显示视图并绘制操纵杆的操纵杆类派生的 除了套接字工作之外,我还试着在Mybot活动中保留所有内容 public class Mybot extends Activity { Thread

我在上一篇文章中犯了一些愚蠢的错误,但我回来了,我觉得我有一个更好的问题

我正在调用一个名为getData的函数,它是一个byte[]返回类型,我正在通过数据包将它发送到另一个程序。在程序端,我收到98 0 0 101,它告诉我bDrive.getX和bDrive.getY函数有问题

这些函数是从我创建的一个显示视图并绘制操纵杆的操纵杆类派生的

除了套接字工作之外,我还试着在Mybot活动中保留所有内容

public class Mybot extends Activity {

Thread arduino;
ArduinoConnection socketThread;
Joystick bDrive;

public void onCreate(Bundle paramBundle)
{
    super.onCreate(paramBundle);
    setContentView(R.layout.activity_Mybot);
    socketThread = new ArduinoConnection(Mybot.this);
    arduino = new Thread( socketThread);
    arduino.start();

}

public byte[] getData() {

    bDrive = (Joystick)findViewById(R.id.bDrive); // I think this is the culprit

    byte[] arrayOfData = new byte[4];

    arrayOfData[0] = 98;
    arrayOfData[1] = (byte) bDrive.getX(); // Returns 0 
    arrayOfData[2] = (byte) bDrive.getY(); // Returns 0
    arrayOfData[3] = 101;

    return arrayOfData;
}
}
我觉得错误在getData函数的findViewById部分中。然而,我在我的操纵杆类中处理了onTouch,所以我真的不明白为什么这会成为一个问题

public class ArduinoConnection implements Runnable {

    boolean sending = true;
    public static DatagramSocket socket;

    Mybot host;

    public ArduinoConnection(Mybot mini) {
        this.host = mini;
    }

    public void run() {

        try {

            socket = new DatagramSocket();
            InetAddress localInetAddress = InetAddress.getByName("192.168.10.104");
            socket.connect(localInetAddress, 61557);
            running = true;

            while (sending) {

                byte[] arrayOfByte = this.host.getData(); // Returning 98 0 0 101

                DatagramPacket localDatagramPacket = new DatagramPacket(arrayOfByte, arrayOfByte.length);
                try {
                    socket.send(localDatagramPacket);
                } catch (IOException localIOException) {
                }

                sending = true;
            }

            socket.close();

        } catch (UnknownHostException localUnknownHostException) {
            sending = false;
        } catch (SocketException e) {
            e.printStackTrace();
            sending = false;
        }

    }

}
Arduino代码是直截了当的

操纵杆类别:

 public class Joystick extends View
{
    private Paint background;
    private String label = null;
    private Paint labelPaint;
    private boolean liftReset = true;
    private Paint line;
    private boolean locked = false;
    private float normX;
    private float normY;
    private int posX;
    private int posY;
    private Paint thumb;
    private int thumbRadius;

    public Joystick(Context paramContext)
    {
        super(paramContext);
    }

    public Joystick(Context paramContext, AttributeSet paramAttributeSet)
    {
        super(paramContext, paramAttributeSet);
        setupJoystick(paramContext, paramAttributeSet);
    }

    public Joystick(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
    {
        super(paramContext, paramAttributeSet, paramInt);
        setupJoystick(paramContext, paramAttributeSet);
    }

    private int min(int paramInt1, int paramInt2)
    {
        if (paramInt1 <= paramInt2) {
            return paramInt1;
        }
        return paramInt2;
    }

    private void updateNormPos()
    {
        this.normX = (-(this.posX - getWidth() / 2) / (getWidth() / 2.0F - this.thumbRadius));
        this.normY = (-(this.posY - getHeight() / 2) / (getHeight() / 2.0F - this.thumbRadius));
    }

    public float getX()
    {
        return this.normX;
    }

    public float getY()
    {
        return this.normY;
    }


    protected Path octagon(int paramInt1, int paramInt2, int paramInt3)
    {
        Path localPath = new Path();
        localPath.moveTo(paramInt2, paramInt3 + paramInt1);
        localPath.lineTo(paramInt2 + 0.866F * paramInt1, paramInt3 + paramInt1 / 2);
        localPath.lineTo(paramInt2 + 0.866F * paramInt1, paramInt3 - paramInt1 / 2);
        localPath.lineTo(paramInt2, paramInt3 - paramInt1);
        localPath.lineTo(paramInt2 - 0.866F * paramInt1, paramInt3 - paramInt1 / 2);
        localPath.lineTo(paramInt2 - 0.866F * paramInt1, paramInt3 + paramInt1 / 2);
        return localPath;
    }

    protected void onDraw(Canvas paramCanvas)
    {
        if ((this.posX == -1) && (this.posY == -1))
        {
            this.posX = (getWidth() / 2);
            this.posY = (getHeight() / 2);
        }
        paramCanvas.drawCircle(getWidth() /2 , getHeight() /2 , 125, this.background);
        if (this.label != null) {
            paramCanvas.drawText(this.label, getWidth() / 2, 40.0F, this.labelPaint);
        }
        paramCanvas.drawLine(getWidth() / 2, getHeight() / 2, this.posX, this.posY, this.line);
        paramCanvas.drawPath(octagon(this.thumbRadius, this.posX, this.posY), this.thumb);
    }

    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        if (this.locked == true) {
            return false;
        }
        if (paramMotionEvent.getAction() == 1 && this.liftReset) {
            this.posX = (getWidth() / 2);
            this.posY = (getHeight() / 2);
        }else {

            for (this.posY = (getHeight() / 2); ; this.posY = min(this.posY, getHeight() - this.thumbRadius)) {
                invalidate();
                updateNormPos();

                double dx = ((int) paramMotionEvent.getX() - getWidth() / 2);
                double dy = ((int) paramMotionEvent.getY() - getHeight() / 2);
                double len = Math.hypot(dx, dy);
                if (len > 100) {
                    dx = dx * 100 / len;
                    dy = dy * 100 / len;
                }
                this.posX = (int) dx + getWidth() / 2;
                this.posY = (int) dy + getHeight() / 2;

                return true;

            }
        }
        return true;
    }

    protected void setupJoystick(Context paramContext, AttributeSet paramAttributeSet)
    {
        int i = 2;
        int j = -16776961;
        int k = -16711936;
        int m = -65536;
        this.shadowRadius = 2;
        this.thumbRadius = 23;
        TypedArray localTypedArray = paramContext.obtainStyledAttributes(paramAttributeSet, R.styleable.Joystick, 0, 0);
        try
        {
            this.thumbRadius = localTypedArray.getDimensionPixelSize(0, 23);
            this.shadowRadius = localTypedArray.getDimensionPixelSize(1, 2);
            i = localTypedArray.getDimensionPixelSize(2, 2);
            m = localTypedArray.getColor(3, -16776961);
            k = localTypedArray.getColor(4, -16777216);
            j = localTypedArray.getColor(5, -7829368);
            this.label = localTypedArray.getString(6);
            this.liftReset = localTypedArray.getBoolean(8, true);
        }
        catch (Exception localException)
        {
            for (;;)
            {
                Log.e("MYAPP", "exception: " + localException.getMessage());
                Log.e("MYAPP", "exception: " + localException.toString());
                localTypedArray.recycle();
            }
        }
        finally
        {
            localTypedArray.recycle();
        }

 // Color in everything
        this.background = new Paint();
        this.background.setColor(k);
        this.background.setStyle(Paint.Style.FILL_AND_STROKE);
        this.thumb = new Paint();
        this.thumb.setAntiAlias(true);
        this.thumb.setColor(m);
        this.thumb.setStyle(Paint.Style.FILL);
        this.line = new Paint();
        this.line.setAntiAlias(true);
        this.line.setColor(j);
        this.line.setStrokeWidth(i);
        this.line.setStyle(Paint.Style.STROKE);
        this.labelPaint = new Paint();
        this.labelPaint.setAntiAlias(true);
        this.labelPaint.setColor(-1);
        this.labelPaint.setStyle(Paint.Style.FILL);
        this.labelPaint.setTextAlign(Paint.Align.CENTER);
        this.labelPaint.setTextSize(30.0F);
        this.labelPaint.setAlpha(128);
        this.posX = -1;
        this.posY = -1;
    }
}
同样,这不是空指针异常,它不会引发任何错误。我可能在某个地方犯了一个小小的错误,但我不明白为什么我得到的是98 0 0 101,而不是98 X坐标的操纵杆Y坐标的操纵杆101


任何帮助都将不胜感激

在哪里重新定位操纵杆视图?如果只翻译视图,可能需要读取getTranslationX和getTranslationY,而不是getX和getY。我不翻译视图,八角形方法绘制实际的操纵杆,其中操纵杆类只是整个对象的视图。getX和getY返回您转换为字节的浮点值。是否返回的值是