1

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.

Community
  • 1
  • 1
eliangela
  • 253
  • 2
  • 5
  • 21

1 Answers1

0

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();  
                }  
            });  
        }  
    }); 
Vito Gentile
  • 13,336
  • 9
  • 61
  • 96