gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:11:23 26 4
gpt4 key购买 nike

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

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

completeMessage.sendToTarget();

提前致谢

完整代码如下:

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;
}
}

最佳答案

如果handler为null,可以使用

Message.obtain()

相反。无论如何,您都需要在发送消息之前检查处理程序是否不为空。

关于java - 使用 handler.obtainmessage 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27820736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com