问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
我想要达到的结果
package com.exae.seabadman;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyCallback;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.*;
import java.util.Random;
public class suolosang extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
new Thread(MyThread).start();
new Thread(ganca).start();
super.onCreate(savedInstanceState);
setContentView(R.layout.suolosang);
ImageView i1, i2;
TextView textView;
float ly, lx;
i2 = findViewById(R.id.i2);
ly = i2.getY();
lx = i2.getX();
textView = findViewById(R.id.textView);
Button b1, b2, b3, b4, b5, b6;
b1 = findViewById(R.id.b1);
b2 = findViewById(R.id.b2);
b3 = findViewById(R.id.b3);
b4 = findViewById(R.id.b4);
b5 = findViewById(R.id.b5);
b6 = findViewById(R.id.b6);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Float ly = i2.getY() ;
if (ly < 0F) {
ly = 0F;
} else {
i2.setY(ly - 40);
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Float ly = i2.getY() ;
if (ly > 1300F) {
ly = 1300F;
} else {
i2.setY(ly + 40);
}
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Float lx = i2.getX() ;
if (lx + 40F < 0) {
lx = 0F;
i2.setX(0);
} else {
i2.setX(lx - 40);
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Float lx = i2.getX() ;
if (lx + 40F >= 1000) {
i2.setX(lx);
} else {
i2.setX(lx + 40);
}
}
});
}
final Handler hans=new Handler() {
@SuppressLint("HandlerLeak")
@Override
public void handleMessage(Message msg) {
ImageView i1;
i1 = findViewById(R.id.i1);
super.handleMessage(msg);
{
if( (msg.what == 80)&&(flag=true)) {
Random llx = new Random();
Random lly = new Random();
Float nnx = llx.nextFloat();
Float nny = lly.nextFloat();
i1.setX(nnx * 800);
i1.setY(nny * 1200);
}
}
}
};
private Runnable MyThread=new Runnable() {
@Override
public void run() {
while (true){
try{
Thread.sleep(1000);
hans.sendMessage(hans.obtainMessage(80));
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
};
Boolean flag=true;
private Runnable ganca=new Runnable() {
@Override
public void run() {
ImageView i1,i2;
i1=findViewById(R.id.i1);
i2=findViewById(R.id.i2);
if((i1.getY()<=i2.getY()+10)&&(i1.getY()>=i2.getY()-10)){
if((i1.getX()<=i2.getX()+10)&&(i1.getX()>=i2.getX()-10)){
flag=false;
}
}
}
};
}
这代码完全不行,也不知道为什么,报错倒是不会,一到模拟器上就会停止运行