显示未捕获异常的消息
我创建了一个类来报告未捕获的异常,如下所示: 如何从 Android 应用程序获取崩溃数据?
我的类:
package br.com.tdta.service.sender;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import br.com.tdta.service.view.MessageDialog;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
/**
*
* @author eliangela
*/
public class ErrorSender implements Thread.UncaughtExceptionHandler {
private Activity activity;
public ErrorSender(Activity activity) {
this.activity = activity;
}
public void sendError(final String error) {
try {
List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add(new BasicNameValuePair("error", error));
HttpPost httppost = new HttpPost("http://192.168.1.1/android/recebeErros.php");
httppost.setEntity(new UrlEncodedFormEntity(values));
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(httppost);
} catch (IOException ex) {
}
}
public void uncaughtException(Thread thread, Throwable trw) {
new Thread(new Runnable() {
public void run() {
MessageDialog.showOkDialog(activity, "Error!\nClick to finish.", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
activity.finish();
}
});
}
}).start();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
trw.printStackTrace(pw);
pw.close();
sendError(sw.toString());
}
}
此类正在向指定的 URL 报告错误。但是,当出现消息对话框时,屏幕没有响应。 如果我不放置消息对话框,屏幕会正常关闭(activity.finish()),但是当我放置消息框时,会出现消息并且屏幕停止响应。
我做错了什么?
多谢。
I created a class to report the uncaught exceptions, like this: How do I obtain crash-data from my Android application?
My class:
package br.com.tdta.service.sender;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import br.com.tdta.service.view.MessageDialog;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
/**
*
* @author eliangela
*/
public class ErrorSender implements Thread.UncaughtExceptionHandler {
private Activity activity;
public ErrorSender(Activity activity) {
this.activity = activity;
}
public void sendError(final String error) {
try {
List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add(new BasicNameValuePair("error", error));
HttpPost httppost = new HttpPost("http://192.168.1.1/android/recebeErros.php");
httppost.setEntity(new UrlEncodedFormEntity(values));
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(httppost);
} catch (IOException ex) {
}
}
public void uncaughtException(Thread thread, Throwable trw) {
new Thread(new Runnable() {
public void run() {
MessageDialog.showOkDialog(activity, "Error!\nClick to finish.", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
activity.finish();
}
});
}
}).start();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
trw.printStackTrace(pw);
pw.close();
sendError(sw.toString());
}
}
This class is reporting the error to the specified URL. But, the screen doesn't respond when the message dialog appears.
If I don't put the message dialog, the screen closes (activity.finish()) normally, but when I put the message box, the message appears and the screen stops responding.
What am I doing wrong?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能 100% 确定这是否有效,但很可能有效。
尝试
用这个代替:
I'm not 100% sure if this works, but it's probably.
Try to substitute this:
with this