使用handler.obtainmessage时出现空指针异常

时间:2015-01-07 13:37:08

标签: java android

我正在使用处理程序将变量值传递给android中的UI线程。然而,应用程序一到达处理程序就会崩溃。任何纠正的想法都会有很大的帮助。这是我的代码:

String var = "NO Keyword Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;

                        completeMessage.sendToTarget();

提前致谢

以下是threD的完整代码:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import android.util.Log;

public class FrameProcessor extends Thread {
    private Uri uri = null;
    private Handler mHandler = null;
    private static volatile boolean bKeepRunning = true;
    private String value = null;
    private Context context = null;

    public FrameProcessor(Object obj, Handler handler, Uri uri) {
        if (obj != null) {
            this.uri = uri;
            this.mHandler = handler;
        }
        this.context = (Context) obj;
    }

    public void setIndex(String val) {
        this.value = val;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void run() {

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();

        retriever.setDataSource(uri.toString(), new HashMap<String, String>());
        Log.v("FrameProcessor", "Video path set.... ");
        ArrayList<Bitmap> rev = new ArrayList<Bitmap>();
        Log.v("FrameProcessor", "uri is: " + uri);
        MediaPlayer mp = MediaPlayer.create(context, uri);
        Log.v("FrameProcessor", "MP created ");
        int millis = mp.getDuration();

        for (int i = 1000000; i < millis * 1000; i += 1000000) {
            Log.v("FrameProcessor", "In Thread's for loop ");
            Bitmap bitmap = retriever.getFrameAtTime(i,
                    MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
            rev.add(bitmap);

            try {
                saveFrames(rev);
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

    }

    @SuppressLint("ShowToast")
    public void saveFrames(ArrayList<Bitmap> saveBitmapList) throws IOException {
        Log.v("FrameProcessor", "In saveFrames ");
        if (value != null) {
            Log.v("FrameProcessor", value);

            if (value.equals("0")) {
                uri = Uri
                        .parse("http://html5dev.anorakstech.com/videos/video.mp4");
            } else if (value.equals("1")) {
                uri = Uri
                        .parse("http://html5dev.anorakstech.com/videos/HSNvideo.mp4");
            } else if (value.equals("2")) {
                uri = Uri
                        .parse("http://html5dev.anorakstech.com/videos/HSNMarkVideo.mp4");
            }
            Log.v("FrameProcessor", uri.toString());
        } else {

        }
        if (value.equals("0")) {
            String folder = Environment.getExternalStorageDirectory()
                    .toString();

            File saveFolder = new File(folder + "/Movies/Frames/");

            if (!saveFolder.exists()) {
                saveFolder.mkdirs();

                Log.v("FrameProcessor", "Extracting Frames.... ");

            }

            int i = 1;
            for (Bitmap b : saveBitmapList) {
                Log.v("FrameProcessor", "In saveFrames for loop");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                File f = new File(saveFolder, ("frame" + i + ".jpg"));

                f.createNewFile();
                if (i >= 0 && i < 6) {

                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i >= 6 && i < 12) {

                    **String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();**
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i >= 12 && i < 18) {
                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;
                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i > 18 && i < 24) {
                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else {

                }
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());

                fo.flush();
                fo.close();

                i++;
            }
        } else if (value.equals("1")) {
            String folder = Environment.getExternalStorageDirectory()
                    .toString();

            File saveFolder = new File(folder + "/Movies/Frames/");

            if (!saveFolder.exists()) {
                saveFolder.mkdirs();

                Log.v("FrameProcessor", "Extracting Frames.... ");

            }

            int i = 1;
            for (Bitmap b : saveBitmapList) {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                File f = new File(saveFolder, ("frame" + i + ".jpg"));

                f.createNewFile();
                if (i >= 0 && i < 6) {

                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i >= 6 && i < 12) {

                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i >= 12 && i < 18) {
                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else if (i > 18 && i < 24) {
                    String var = "NO Keyword Found";
                    Message completeMessage = mHandler.obtainMessage();
                    completeMessage.obj = var;

                    completeMessage.sendToTarget();
                    Log.v("FrameProcessor", "NO Keyword Found");

                } else {

                }
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());

                fo.flush();
                fo.close();

                i++;
            }
        } else {
            String folder = Environment.getExternalStorageDirectory()
                    .toString();

            File saveFolder = new File(folder + "/Movies/Frames/");

            if (!saveFolder.exists()) {
                saveFolder.mkdirs();

                Log.v("FrameProcessor", "Extracting Frames.... ");

            }

            int i = 1;
            while (bKeepRunning) {

                for (Bitmap b : saveBitmapList) {
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    b.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                    File f = new File(saveFolder, ("frame" + i + ".jpg"));

                    f.createNewFile();
                    if (i >= 0 && i < 6) {

                        String var = "Keyword HSN Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;

                        completeMessage.sendToTarget();
                        Log.v("FrameProcessor", "Keyword ABCFound");

                    } else if (i >= 6 && i < 12) {

                        String var = "Keyword HSN Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;
                        Log.v("FrameProcessor", "Keyword SHOP Found");

                    } else if (i >= 12 && i < 18) {
                        String var = "Keyword HSN Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;
                        Log.v("FrameProcessor", "Keyword BY Found");

                    } else if (i > 18 && i < 24) {
                        String var = "Keyword HSN Found";
                        Message completeMessage = mHandler.obtainMessage();
                        completeMessage.obj = var;
                        Log.v("FrameProcessor", "Keyword REMOTE Found");

                    } else {

                    }
                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());

                    fo.flush();
                    fo.close();

                    i++;
                }
            }
        }
    }

    public void kill() {
        bKeepRunning = false;
    }
}

1 个答案:

答案 0 :(得分:1)

如果handler为null,则可以使用

Message.obtain()

代替。无论如何,您将需要在发送消息之前检查处理程序是否为空。