当我尝试在我创建的AsyncTask上执行时,我的应用程序崩溃了。
基本上我正在尝试让我的Task从给定的ResID解码位图,并通过传递容器参数在画布上绘制它。
以下是代码的相关部分:
class BitmapWorker extends AsyncTask<MyTaskParams, Void, Void> {
private Canvas canvas;
private Paint paint;
private BitmapFactory.Options opt;
private View view;
public BitmapWorker(Canvas canvas, Paint paint, View view) {
this.canvas = canvas;
this.paint = paint;
this.view = view;
}
@Override
protected Void doInBackground(MyTaskParams... params) {
Bitmap bmp;
opt.inJustDecodeBounds = true;
opt = new BitmapFactory.Options();
opt.inPurgeable = true;
opt.inSampleSize = calculateInSampleSize(opt, canvas.getWidth() / 16, canvas.getHeight() / 10);
opt.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeResource(getResources(), params[0].resID,opt);
canvas.drawBitmap(bmp, params[0].x, params[0].y, paint);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
view.invalidate();
}
}
......
public class mbbs extends SurfaceView implements Runnable {
....
@Override
public void run() {
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(30);
while (running) {
if (!holder.getSurface().isValid())
continue;
Canvas canvas = holder.lockCanvas();
canvas.drawColor(Color.WHITE);
BitmapWorker task=new BitmapWorker(canvas,paint,surfaceview);
MyTaskParams params=new MyTaskParams((int)crossx,(int)crossy,R.drawable.crosshair);
task.execute(params); //Crashes here
logcat打印出错误异常,如下所示:
12-06 15:25:46.829 1632-1645/android.process.acore E/DictionaryBackupAgent﹕ Couldn't read from the cursor
12-06 15:26:34.228 1632-1641/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:57)
at android.os.Binder.execTransact(Binder.java:446)
12-06 15:26:34.229 1632-1641/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:64)
at android.os.Binder.execTransact(Binder.java:446)
12-06 15:26:34.229 1632-1641/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:71)
at android.os.Binder.execTransact(Binder.java:446)
我尝试使用谷歌,但所有这些都是关于严格模式或不释放任何资源的。但是,我不确定我应该发布什么资源。
感谢。