【发布时间】:2012-04-26 08:10:53
【问题描述】:
我正在使用未捕获的异常处理程序来捕获异常,但这会导致我的应用程序停止。我指的是这个帖子 How do I stop my application from zombifying after I handle an uncaught Excepition? 还有这个Ideal way to set global uncaught exception Handler in 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)]
不知道是什么问题,可能是
- 我的线程崩溃了
- 我的处理程序没有被调用
- 或者它没有把异常放在前面,但它导致我的应用程序停止
- 异常会导致以下可能性崩溃
谁能帮我防止这种停顿
【问题讨论】:
-
不知道你在问什么或想做什么。
-
你不能缩进你的代码吗? (n eclipse 选择块,使用源菜单将其向右移动并将其粘贴到您的问题中)
-
另外,请提供堆栈跟踪
-
@Snicolas 好的,我给你的是代码活动类
-
@Snicolas 是的,我已经根据您的规范更新了我的答案我现在能做什么?
标签: android exception uncaughtexceptionhandler