gpt4 book ai didi

android - 使用未捕获的异常处理程序时应用程序崩溃导致应用程序停止

转载 作者:搜寻专家 更新时间:2023-11-01 09:08:12 27 4
gpt4 key购买 nike

我正在使用未捕获的异常处理程序来捕获异常,但它导致我的应用程序停止。我指的是这篇文章 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");

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

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. 或者它没有异常(exception)但它导致我的应用程序停止
  4. Exception 使它在以下可能性下崩溃

谁能帮我防止这种情况发生

最佳答案

使用 ACRA (http://code.google.com/p/acra/)!他们会做您需要的,并解决了您遇到的问题

关于android - 使用未捕获的异常处理程序时应用程序崩溃导致应用程序停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10329513/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com