我想获得触摸输入的x和y坐标。我试图这样做:
package com.example.benjamin.labb3;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import android.view.View.OnTouchListener;
public class Main extends Activity implements OnTouchListener {
DrawView drawView;
String inputAction = "";
Point touch;
@Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
drawView = new DrawView(this);
setContentView(drawView);
drawView.setOnTouchListener(this);
}
@Override public void onResume(){
super.onResume();
drawView.resume();
}
@Override public void onPause(){
super.onPause();
drawView.pause();
}
public boolean onTouch (View v, MotionEvent event) {
touch.x = (int)event.getX();
touch.y= (int)event.getY();
return true;
}
public class DrawView extends SurfaceView implements Runnable {
Thread gameloop = null;
SurfaceHolder surface;
volatile boolean running = false;
AssetManager assets = null;
BitmapFactory.Options options = null;
Bitmap incect[];
int frame = 0;
public DrawView(Context context){
super(context);
surface = getHolder();
assets = context.getAssets();
options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
incect = new Bitmap[2];
try {
for (int n = 0; n < 2; n++){
String fileName = "incect"+Integer.toString(n+1)+".png";
InputStream istream = assets.open(fileName);
incect[n] = BitmapFactory.decodeStream(istream,null,options);
istream.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
public void resume() {
running = true;
gameloop = new Thread(this);
gameloop.start();
}
public void pause() {
running = false;
boolean retry = true;
while (retry) {
try {
gameloop.join();
retry = false;
} catch (InterruptedException e){}
}
}
@Override public void run(){
Point size = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getSize(size);
Random wR = new Random();
Random hR = new Random();
int width = size.x;
int height = size.y;
int randomW = wR.nextInt(width-20)+20;
int randomH = hR.nextInt(height-20)+20;
while (running){
if(!surface.getSurface().isValid())
continue;
Canvas canvas = surface.lockCanvas();
canvas.drawColor(Color.rgb(85,107,47));
canvas.drawBitmap(incect[frame],randomW-10,randomH-10,null);
surface.unlockCanvasAndPost(canvas);
frame ++;
if (frame > 1){
frame = 0;
}
try {
Thread.sleep(500);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
}
但是当我尝试在手机上运行应用程序(并触摸屏幕)时,我会被送回上一个活动。截至目前,我无法使用android studio中的日志,所以我理解这是一个难以回答的问题。
答案 0 :(得分:2)
你的Point
触摸一直是NULL,不是吗?
可能你忘了初始化它:
touch = new Point();
没有这一行,变量touch
一直是null,所以当调用onTouch()时,Logcat会说:
java.lang.NullPointerException:尝试在空对象引用上写入'int android.graphics.Point.x'字段
所以在onCreate()
中添加以上行,或在调用onTouch()
之前可以初始化它,
希望这会有所帮助。