我在上一篇文章中犯了一些愚蠢的错误,但我回来了,我觉得我有一个更好的格式化问题。
我正在调用一个名为getData()
的函数,它是一个byte[]
返回类型,我将它通过数据包发送到另一个程序。在程序方面,我收到“98 0 0 101”,它告诉我bDrive.getX()
和bDrive.getY()
函数出了问题。
这些函数来自我制作的Joystick类,它显示了一个View并绘制了一个操纵杆。 p>
除了套接字工作之外,我已经尝试将所有内容保留在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;
}
}
我觉得错误在findViewById()
函数的getData()
部分内。但是我在Joystick类中处理了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。
任何帮助将不胜感激!