Reputation: 305
I want to get the x and y coords of the touch input. I am trying to do this using:
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();
}
}
}
}
}
But when i try to run the application on my phone (and touch the screen) i get sent back to the previous activity. As of now i cant use the log in android studio so i understand if it is a difficult question to answer.
Upvotes: 2
Views: 503
Reputation: 386
Your Point
touch is NULL all the time isn't it ?
Probably you forgot to initialize it :
touch = new Point();
without this line, the variable touch
is null all the time, so when onTouch() is called, Logcat will say :
java.lang.NullPointerException: Attempt to write to field 'int android.graphics.Point.x' on a null object reference
So add the above line in onCreate()
or somewhere it can be initialized before onTouch()
is called,
Hope this helps.
Upvotes: 2