显示未捕获异常的消息

发布于 2024-12-08 20:01:39 字数 2483 浏览 0 评论 0原文

我创建了一个类来报告未捕获的异常,如下所示: 如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青芜 2024-12-15 20:01:39

我不能 100% 确定这是否有效,但很可能有效。
尝试

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

I'm not 100% sure if this works, but it's probably.
Try to substitute this:

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

with this

    runOnUiThread(new Runnable() {  

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

                public void onClick(DialogInterface arg0, int arg1) {  
                    activity.finish();  
                }  
            });  
        }  
    }); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文