问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
java
package com.exae.seabadman;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.content.Intent;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.view.View.OnTouchListener;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class xiangjishi extends AppCompatActivity {
private int startl,startr,startt,startb,lastX;
@Override
public void onCreate(Bundle savedInstanceState) {
new Thread(qq).start();
//ImageView akumo, xiogishiyo;
// akumo = findViewById(R.id.akumo);
//xiogishiyo=findViewById(R.id.xiogishi);
super.onCreate(savedInstanceState);
setContentView(R.layout.xiangjishiyoshi);
DisplayMetrics dm = getResources().getDisplayMetrics();
final int screenWidth = dm.widthPixels;
final ImageButton direction = (ImageButton) findViewById(R.id.direction);
// xx=xiogishiyo.getX();
//dx = (int) direction.getX();
direction.setOnTouchListener(new View.OnTouchListener() {
int lastX, lastY;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
int ea = motionEvent.getAction();
switch (ea) {
case MotionEvent.ACTION_DOWN:
startl = direction.getLeft();
startr = direction.getRight();
startt = direction.getTop();
startb = direction.getBottom();
lastX = x;
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) motionEvent.getX() - lastX;
int l = view.getLeft() + dx;
int b = view.getBottom();
int r = view.getRight() + dx;
int t = view.getTop();
if (l < 0) {
l = 0;
r = l + view.getWidth();
}
if (r > screenWidth) {
r = screenWidth;
l = r - view.getWidth();
}
view.layout(l, t, r, b);
lastX = (int) motionEvent.getRawX();
view.postInvalidate();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
}
final Handler hans = new Handler() {
Float dx;
ImageView xiogishiyo;
ImageButton direction;
@Override
public void handleMessage(Message msg) {
xiogishiyo = findViewById(R.id.xiogishi);
direction = findViewById(R.id.direction);
super.handleMessage(msg);
{
if (msg.what == 80) {
dx = direction.getX();
xiogishiyo.setX(dx);
}
}
}
};
private Runnable qq=new Runnable() {
@Override
public void run() {
try {
while (true) {
hans.sendMessage(hans.obtainMessage(80));
}
}catch (Exception e){
e.printStackTrace();
}
}
};
}
java
请指出这代码不能在模拟器上运行的根本原因,并给出切实可行的解决方法,