Can anyone help me solve the error in my asynctask? I think I know why it gives an error but I don't know what to do to solve it. Thank you very much.
here is my asynctask code (just want to use it for a joystick using surfaceview)
public class MySurfaceThread extends AsyncTask<Void, Void, Void> {
SurfaceHolder mSurfaceHolder;
CustomSurfaceView cSurfaceView;
public MySurfaceThread(SurfaceHolder sh, CustomSurfaceView csv) {
mSurfaceHolder = sh;
cSurfaceView = csv;
x = y = 0;
cSurfaceView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
calculateValues(x, y);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
x = y = 0;
dx = dy = 0;
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
private void calculateValues(float xx, float yy) {
dx = xx - zeroX;
dy = yy - zeroY;
angle = (float)Math.atan(Math.abs(dy/dx));
hyp = (float)Math.sqrt((dx * dx) + (dy * dy));
if(hyp > radius) {
if(dx > 0 && dy > 0){ //bottom right
xx = (zeroX + (radius*(float)Math.cos(angle)));
yy = (zeroY + (radius*(float)Math.sin(angle)));
}
else if(dx > 0 && dy < 0){ //top right
xx = (zeroX + (radius*(float)Math.cos(angle)));
yy = (zeroY - (radius*(float)Math.sin(angle)));
}
else if(dx < 0 && dy < 0){ //top left
xx = (zeroX - (radius*(float)Math.cos(angle)));
yy = (zeroY - (radius*(float)Math.sin(angle)));
}
else if(dx < 0 && dy > 0) { //bottom left
xx = (zeroX - (radius*(float)Math.cos(angle)));
yy = (zeroY + (radius*(float)Math.sin(angle)));
}
}
else {
xx = zeroX + dx;
yy = zeroY + dy;
}
x = xx;
y = yy;
}
});
}
@Override
protected Void doInBackground(Void... params) {
Canvas canvas = null;
while (running) {
try {
canvas = mSurfaceHolder.lockCanvas();
synchronized (mSurfaceHolder) {
zeroX = canvas.getWidth()/2;
zeroY = canvas.getHeight()/2;
cSurfaceView.onDraw(canvas, x, y, zeroX, zeroY);
}
Thread.sleep(10);
} catch (InterruptedException e) {
} finally {
if (canvas != null) {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
return null;
}
}
Can I do the same task using runnable and thread? If so, how? thank you.
EDIT
Here is the error message in the logcat:
03-09 15:23:52.394 4135-4159/com.example.r.bluetoothsample E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.r.bluetoothsample, PID: 4135
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.r.bluetoothsample.CustomSurfaceView$MySurfaceThread.doInBackground(CustomSurfaceView.java:182)
at com.example.r.bluetoothsample.CustomSurfaceView$MySurfaceThread.doInBackground(CustomSurfaceView.java:95)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
lockCanvas()
will return null if your surface is unavailable (which I assume it becomes when you leave your activity). That means you'll run into a NullPointerException
at your getWidth()
call.
On a side note, I hope you're stopping that background loop (maybe by setting running
to false
?) in your activity's onPause()
method -- otherwise, it'll keep looping in the background, which is undesirable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.