7

我只是在Activity上调用isDestroyed(),我得到了这个 ex:

04-09 03:08:12.692: E/AndroidRuntime(13234): FATAL EXCEPTION: main
04-09 03:08:12.692: E/AndroidRuntime(13234): java.lang.NoSuchMethodError: android.app.Activity.isDestroyed
04-09 03:08:12.692: E/AndroidRuntime(13234):    at hu.illion.beentaps.util.ActivityKiller.killAllPastActivites(ActivityKiller.java:16)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at hu.illion.beentaps.MapBeenActivity$1.onClick(MapBeenActivity.java:75)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at android.view.View.performClick(View.java:4084)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at android.view.View$PerformClick.run(View.java:16966)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at android.os.Handler.handleCallback(Handler.java:615)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at android.os.Looper.loop(Looper.java:137)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at android.app.ActivityThread.main(ActivityThread.java:4931)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at java.lang.reflect.Method.invoke(Method.java:511)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-09 03:08:12.692: E/AndroidRuntime(13234):    at dalvik.system.NativeStart.main(Native Method)

我在列表中有活动,我想遍历它并完成所有活动:

for (Activity act : Variables.pastActivites) {
        try {

            Log.i("Killing: ", act.getLocalClassName());
            if (!act.isDestroyed()) {
                act.overridePendingTransition(0, 0);
                act.finish();
            }
            else
            {
                Variables.pastActivites.remove(act);
            }

        } catch (Exception ex) {
            Log.i("KillerAct: ", ex.toString());
        }

    }

我什至可以阅读官方文档,其中有一个名为 isDestroyed() 的函数。现在怎么办?

4

3 回答 3

21

Activity.isDestroyed()从 API 级别 17 开始可用。如果您的应用程序设置适用于较低的 API,您将获得此Exception.

于 2013-04-09T01:31:10.080 回答
6

你可以这样写来避免这个问题:

if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
    if (!act.isDestroyed()) {
        act.overridePendingTransition(0, 0);
        act.finish();
    }
}
于 2015-01-15T07:11:22.247 回答
3

根据Activity.onDestroyed(),这在 API 级别 17 及更高版本中可用,这是最新的 Android 版本 4.2+

您尝试在哪个 Android 版本上运行此代码?

于 2013-04-09T01:32:27.867 回答