如何解决无法在给定的 android 代码段中暂停 activity?

How to resolve unable to pause activity in the given android code snippet?

以下是 android 代码段

runOnUiThread(new Runnable() {
                                public void run() {
                                    AlertDialog alertDialog;
                                    alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                                    alertDialog.setTitle("Network error");
                                    alertDialog.setMessage("Disconnected");
                                    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
                                        /** ON clicking oKay button **/
                                        public void onClick(DialogInterface dialog, int id)
                                        {

                                            hasConnectServiceStarted = false;
                                            IP = null;
//                                            Intent intent = getIntent();
                                            Intent intentSplash = new Intent(getApplication().getApplicationContext(), SplashActivity.class);

                                            /** Shutdown the nodemain executor service if
                                             * HOST goes down
                                             */
                                            nodeMainExecutorService.forceShutdown();
                                            /** and restart the Main Activity that will prompt
                                             * user to reconnect to the HOST once again
                                             */

//  I want current activity to destroy before starting Splash Activity, hence calling onDestroy()
                                            onDestroy();
                                            startActivity(intentSplash);

                                        }
                                    });
                                    alertDialog.show();

});

一切似乎都很好,但是当出现此警告对话框并单击“确定”时,出现以下错误:

java.lang.RuntimeException: Unable to pause activity {edu.arizona.ece573.catgraph/edu.arizona.ece573.catgraph.MainActivity}: java.lang.IllegalStateException: No host at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3293) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3252) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3227) at android.app.ActivityThread.access00(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5292) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) Caused by: java.lang.IllegalStateException: No host at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1235) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230) at android.support.v4.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:2056) at android.support.v4.app.FragmentController.dispatchPause(FragmentController.java:198) at android.support.v4.app.FragmentActivity.onPause(FragmentActivity.java:401) at android.app.Activity.performPause(Activity.java:6101) at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1310) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3279) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3252)  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3227)  at android.app.ActivityThread.access00(ActivityThread.java:154)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5292)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

我该如何解决这个错误?

只需在 startActivity(intentSplash); 之后调用 finish() 并删除 destroy() 即可实现电流 activity 将被破坏。

How should I resolve this error?

替换对

的调用

onDestroy()

finish()

您还应该检查 documentation 以了解哪个方法执行什么操作。