2

我正在使用未捕获的异常处理程序来捕获异常,但这会导致我的应用程序停止。我指的是这篇文章 如何在处理未捕获的异常后阻止我的应用程序僵尸化? 以及在Android中设置全局未捕获异常处理程序的理想方式

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.advsearch);
        setTitle("Advance Search");


    Thread.setDefaultUncaughtExceptionHandler(new SRSDexception(this));


    String trace = null;
    String line;
    try {
        BufferedReader reader = new BufferedReader(
        new InputStreamReader(SRSDAdvSearch.this
        .openFileInput("stack.trace")));

        while((line = reader.readLine()) != null) {
        trace += line+"\n";
        }
        } catch(FileNotFoundException fnfe) {
        // ...
        } catch(IOException ioe) {
        // ...
        }

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        String subject = "Error report";
        String body =
        "Mail this to readerscope@altcanvas.com: "+
        "\n\n"+
        trace+
        "\n\n";

        sendIntent.putExtra(Intent.EXTRA_EMAIL,
        new String[] {"readerscope@altcanvas.com"});
        sendIntent.putExtra(Intent.EXTRA_TEXT, body);
        sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        sendIntent.setType("message/rfc822");

        SRSDAdvSearch.this.startActivity(
        Intent.createChooser(sendIntent, "Title:"));

        SRSDAdvSearch.this.deleteFile("stack.trace");

这是我的活动类,它又调用了我的异常类阅读完网址后,您将了解我想要什么..我已经复制了未捕获的异常,即

import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;

public class SRSDexception implements Thread.UncaughtExceptionHandler {

private Thread.UncaughtExceptionHandler defaultUEH;

private Activity app = null;

public SRSDexception(Activity app) {
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
this.app = app;

}

public void uncaughtException(Thread t, Throwable e) 
{   
//  System.out.println("You crashed thread " + t.getName());
//    System.out.println("Exception was: " + e.toString());
StackTraceElement[] arr = e.getStackTrace();
String Raghav =t.toString();
String report = e.toString()+"\n\n";
report += "--------- Stack trace ---------\n\n"+Raghav;
for (int i=0; i<arr.length; i++)
{
report += "    "+arr[i].toString()+"\n";
}
report += "-------------------------------\n\n";

// If the exception was thrown in a background thread inside
// AsyncTask, then the actual exception can be found with getCause
report += "--------- Cause ---------\n\n";
Throwable cause = e.getCause();
if(cause != null) {
report += cause.toString() + "\n\n";
arr = cause.getStackTrace();
for (int i=0; i<arr.length; i++)
{
report += "    "+arr[i].toString()+"\n";
}
}
report += "-------------------------------\n\n";

try {
FileOutputStream trace = app.openFileOutput(
"stack.trace", Context.MODE_PRIVATE);
trace.write(report.getBytes());
trace.close();
} catch(IOException ioe) {
// ...
}

defaultUEH.uncaughtException(t, e);
}

堆栈跟踪

[android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314),   
SRTekBox.Android.SRSD.SRSDAdvSearch.fnExpand(SRSDAdvSearch.java:390),  
SRTekBox.Android.SRSD.SRSDAdvSearch$5.onClick(SRSDAdvSearch.java:209), 
android.view.View.performClick(View.java:2408), android.view.View$PerformClick.run (View.java:8816),
android.os.Handler.handleCallback(Handler.java:587),
android.os.Handler.dispatchMessage(Handler.java:92),
android.os.Looper.loop(Looper.java:123),
android.app.ActivityThread.main(ActivityThread.java:4633),
java.lang.reflect.Method.invokeNative(Native Method),
java.lang.reflect.Method.invoke(Method.java:521),
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858),
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616),
dalvik.system.NativeStart.main(Native Method)]

不知道是什么问题,可能是

  1. 我的线程正在崩溃
  2. 我的处理程序没有被调用
  3. 或者它没有例外,但它导致我的申请暂停
  4. 异常使其崩溃以下可能性

谁能帮我防止这种停止

4

1 回答 1

3

使用 ACRA (http://code.google.com/p/acra/) !他们做你需要的,并解决了你正在经历的问题

于 2012-04-26T14:02:45.573 回答