显示未捕获异常的消息

时间:2011-10-06 21:18:18

标签: android uncaught-exception

我创建了一个类来报告未捕获的异常,例如:How do I obtain crash-data from my Android application?

我的课程:

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()),但是当我放入消息框时,会出现消息并且屏幕停止响应。

我做错了什么?

非常感谢。

1 个答案:

答案 0 :(得分:0)

我不是百分百确定这是否有效,但可能是这样。 试着用这个代替:

    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(); 

用这个

    runOnUiThread(new Runnable() {  

        public void run() {  
            MessageDialog.showOkDialog(activity, "Error!\nClick to finish.", new OnClickListener() {  

                public void onClick(DialogInterface arg0, int arg1) {  
                    activity.finish();  
                }  
            });  
        }  
    });