调用AsyncTask时出现NullPointerException

时间:2013-06-19 08:18:49

标签: java android android-asynctask nullpointerexception

是通过AsyncTask裁剪图像的代码:

package com.mmbang.weather;

import java.io.FileNotFoundException;
import java.io.IOException;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

public class CameraImageAsyncTask extends AsyncTask<Uri, ImageView, Bitmap> {

    private Context context;
    private Uri uri;
    private Bitmap bmp = null;
    private LocalHandler handler;
    private int flag;
    private String newPath;
    private boolean isTooSmall = false;
    private Runnable runable;

    // private ImageView iv;

    public CameraImageAsyncTask(Context context, Uri uri, int flag, LocalHandler handler) {
        this.context = context;
        this.uri = uri;
        this.flag = flag;
        this.handler = handler;
    }

    public void setRunable(Runnable run) {
        runable = run;
    }

    @Override
    protected Bitmap doInBackground(Uri... params) {

        try {
            bmp = BitmapUtil.getLoadImage(context, uri);
            if (bmp != null) {
                float scale = 1.0f;
                if (flag == 1 && bmp.getHeight() < 160 && bmp.getWidth() < 160) {
                    scale = 1.3f;
                } else if (bmp.getHeight() > 800 && bmp.getWidth() > 800) {
                    scale = 0.7f;
                }
                Bitmap newBmp = BitmapUtil.scaleToNewBitmap(bmp, scale);
                newPath = BitmapUtil.saveBitmapToJpgFile(newBmp, System.currentTimeMillis() + "");
                // bmp.recycle();
                return newBmp;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        // this.iv.setImageBitmap(result);
        Message msg = new Message();
        BitmapItem bi = new BitmapItem();
        bi.bmp = result;
        bi.path = newPath;
        msg.what = flag;
        msg.obj = bi;
        msg.arg1 = (isTooSmall == true ? 1 : 0);

        Log.i("xxxxx",String.valueOf(msg.what));
        Log.i("yyy",bi.path);

        handler.sendMessage(msg);
        Activity activity = (Activity) context;
        if (runable != null) activity.runOnUiThread(runable);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(ImageView... values) {

        super.onProgressUpdate(values);
    }



    public class BitmapItem {
        public Bitmap bmp;
        public String path;
    }

}

我称之为:

new CameraImageAsyncTask(this, Uri.fromFile(photoFile), 1, imageTaskHandle).execute();

我得到了这个例外:

06-19 16:17:18.979: E/AndroidRuntime(19184): java.lang.NullPointerException
06-19 16:17:18.979: E/AndroidRuntime(19184):    at com.mmbang.weather.CameraImageAsyncTask.onPostExecute(CameraImageAsyncTask.java:86)
06-19 16:17:18.979: E/AndroidRuntime(19184):    at com.mmbang.weather.CameraImageAsyncTask.onPostExecute(CameraImageAsyncTask.java:1)

为什么会这样?非常感谢

0 个答案:

没有答案