从父活动中的子活动捕获异常

RdP*_*dPC 11 android

我一直想知道是否可以通过捕获父活动中的崩溃来阻止Android应用程序崩溃.

假设我在子活动的onCreate方法中导致致命异常,我能否以任何方式捕获该异常?或者无论我尝试什么,应用程序都会崩溃?

这是我的意思的一个例子:

Main.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ly_main);
    // My Main activity starts
    try{
        // Call the next activity
        Intent intent = new Intent(getApplicationContext(), Child.class);
        startActivity(intent);
    }catch(Exception e){
        Log.wtf("Exception_WTF","Exception from child activity woohoo \n "+ e.toString());
    }
Run Code Online (Sandbox Code Playgroud)

Child.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ly_child);
    // Create exception... for science
    int a = 0;
    a = 1/a;
}
Run Code Online (Sandbox Code Playgroud)

这不起作用.子活动死亡并将父母带走.

是否可以通过startActivityForResult来实现?

谢谢,

编辑:我不需要崩溃数据,我只是想知道如何避免应用程序崩溃.

环顾四周我发现: 在android上使用全局异常处理

其中包括这件作品:

   Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
            Log.e("Alert","Lets See if it Works !!!");
        }
    });
Run Code Online (Sandbox Code Playgroud)

那让我记录uncaughtException,避免"崩溃",然而,应用程序黑屏并停止响应...

编辑2:在线程中经过大量阅读(感谢user370305)如何从Android应用程序中获取崩溃数据?

我已达到死胡同,要么处理uncaughtException并调用defaultUEH.uncaughtException(paramThread,paramThrowable); 所以应用程序崩溃,或者我没有调用defaultUEH.uncaughtException,应用程序没有崩溃,但也没有响应...任何想法?

final Thread.UncaughtExceptionHandler defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
        Log.e("Alert","Lets See if it Works !!!");
        defaultUEH.uncaughtException(paramThread, paramThrowable);
    });
Run Code Online (Sandbox Code Playgroud)

cal*_*ari 6

Android中的活动必须独立管理,因为它们有自己的生命周期.因此, 只在生成它们的活动中捕获异常.

如果您的活动需要交互以返回先前的用户活动,请完成捕获异常(子)的活动,并让之前的活动(父)知道结果.请参阅启动活动和获取结果作为相互传达父项和子项活动的方法.


R4j*_*R4j 6

希望我正确理解你想要什么。您可以查看FBReaderJ的源代码,它是处理您问题的真实示例。他们可以做得很好:每当应用程序出现错误时,他们都会向用户显示错误报告对话框,他们可以通过读取Throwable信息来过滤错误。
例如,看看BookInfoActivity,他们像这样注册了异常处理程序:

Thread.setDefaultUncaughtExceptionHandler(
        new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this)
    );
Run Code Online (Sandbox Code Playgroud)

然后UncaughtExceptionHandler类将处理错误:

  @Override
public void uncaughtException(Thread thread, Throwable exception) {
    final StringWriter stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(stackTrace));
    System.err.println(stackTrace);

    if(myContext != null && myContext instanceof Activity){
        ((Activity)myContext).finish();
        return;
    }


    // filter the error by get throwable class name and put them into 
    // intent action which registered in manifest with particular handle activity.
    Intent intent = new Intent(
        "android.fbreader.action.CRASH",
        new Uri.Builder().scheme(exception.getClass().getSimpleName()).build()
    );
    try {
        myContext.startActivity(intent);
    } catch (ActivityNotFoundException e) {
    // or just go to the handle bug activity
        intent = new Intent(myContext, BugReportActivity.class);
        intent.putExtra(BugReportActivity.STACKTRACE, stackTrace.toString());
        myContext.startActivity(intent);
    }

    if (myContext instanceof Activity) {
        ((Activity)myContext).finish();
    }

        // kill the error thread
        Process.killProcess(Process.myPid());
    System.exit(10);
}
Run Code Online (Sandbox Code Playgroud)

希望这能有所帮助。


Mr.*_*Rao 0

如果您捕获所有可能的异常并提供适当的操作,您就可以停止崩溃。

关于捕获父级异常:为什么要这样做?您可以捕获子活动中的异常,并通过意图将必要的信息发送到父活动。我认为不可能在父级中捕获子级异常。