未捕获的异常重新发起呼叫活动? [英] uncaught exception relaunching the calling activity?

查看:118
本文介绍了未捕获的异常重新发起呼叫活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的Andr​​oid应用程序,最近我看到,在任何未捕获异常,显示在DDMS logcat的异常,并以下主叫父活动被重新启动(GET的onCreate叫)。理想情况下,应用程序应该只是退出。

I have a large android app and recently I am seeing that on any uncaught exception, the exception is shown in the DDMS Logcat and following that the calling parent activity gets relaunched (onCreate get's called). Ideally the application should just exit.

我们正在使用ARCA崩溃的应用程序的报告,但评论指出,仍然可以看到同样的活动得到重新启动。我打电话的startActivityForResults和Android清单有机器人:该活动finishOnTaskLaunch也是如此的子活动。

We are using ARCA crash application reporting, but commented out that and still see the same activity getting relaunched. I am calling the startActivityForResults and the android manifest has android:finishOnTaskLaunch true for the activity as well as the subactivity.

这是什么可能会造成异常的活动重新发布任何指针?

Any pointers on what could be causing the activity relaunch on the exception?

推荐答案

我相信这是强制关闭的预期行为。用户得到通知的例外,当他们承认这一点,系统会尝试让他们回来尽可能接近他们目前的状态,也就是最后那个工作的活动。

I believe this is the expected behavior of force closes. The user gets informed of the exception, when they acknowledge it the system tries to get them back as close as possible to their current state, i.e., the last activity that worked.

我不知道为什么这种行为将不被需要的,但你可能需要拿出自己认识到的活动是因为的onCreate崩溃,并立即退出重新启动的方法。

I'm not sure why this behavior wouldn't be desired, but you probably need to come up with your own method of recognizing that the activity was restarted because of a crash and immediately exit in onCreate.

编辑:
我只是把一个测试程序:

I just put together a test app:

public class Activity1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button)findViewById(R.id.button);
        button.setText("test");
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity1.this, Activity2.class);
                startActivityForResult(intent, 0);
            }
        });
    }
}

public class Activity2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button button = (Button)findViewById(R.id.button);
        button.setText("test2");
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                throw new RuntimeException();
            }
        });
    }
}

当你的Test2按下按钮时,会抛出异常。 Android的显示了强制关闭对话框,当你在对话框中单击确定,它需要你回测试。这就是我的意思的默认行为。

When you push the button in Test2, the exception is thrown. Android shows a force close dialog, and when you click OK in the dialog, it takes you back to Test. That is what I meant by the default behavior.

应对这种情况的最好办法是解决您的应用程序,以便它永远不会抛出异常。

The best way to deal with this is to fix your app so that it never throws an exception.

作为最后的手段,您可以添加一个处理程序来处理未捕获的异常:<一href=\"http://developer.android.com/reference/java/lang/Thread.html#setDefaultUncaughtExceptionHandler%28java.lang.Thread.UncaughtExceptionHandler%29\"相对=nofollow> UncaughtExceptionHandler的。这prevents从首位被显示的部队密切对话,所以,只要你喜欢,你可以做。

As a last resort, you could add a handler for dealing with uncaught exceptions: uncaughtExceptionHandler. This prevents the force close dialog from being shown in the first place, so you can do as you like.

这篇关于未捕获的异常重新发起呼叫活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆