MapSupportFragment - IllegalStateException - 无主机

时间:2016-03-11 04:57:23

标签: android supportmapfragment

我这样使用SupportMapFragment。

我的活动extends FragmentActivity

在onCreate

((SupportMapFragment) getSupportFragmentManager().findFragmentById(
            R.id.locationMap)).getMapAsync(this);

onMapReady

@Override
public void onMapReady(GoogleMap locationMap) {
    this.locationMap = locationMap;
    UiSettings mapSettings = locationMap.getUiSettings();
    locationMap.setTrafficEnabled(true);
    mapSettings.setAllGesturesEnabled(true);
    mapSettings.setCompassEnabled(true);
    mapSettings.setMyLocationButtonEnabled(true);
    mapSettings.setRotateGesturesEnabled(true);
    mapSettings.setScrollGesturesEnabled(true);
    mapSettings.setTiltGesturesEnabled(false);
    mapSettings.setZoomControlsEnabled(true);
    mapSettings.setZoomGesturesEnabled(true);
    locationMap.animateCamera(CameraUpdateFactory.zoomTo(17), 2000, null);
    locationMap.setMyLocationEnabled(true);
}

现在,当我退出应用程序并重新加载它时,我得到了

java.lang.RuntimeException: Unable to pause activity {com.mcruiseon.hindjewadipune.caregiri/com.mcruiseon.caregiri.Home}: java.lang.IllegalStateException: No host
1   at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
2   at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
3   at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
4   at android.app.ActivityThread.access$1700(ActivityThread.java:117)
5   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:946)
6   at android.os.Handler.dispatchMessage(Handler.java:99)
7   at android.os.Looper.loop(Looper.java:130)
8   at android.app.ActivityThread.main(ActivityThread.java:3687)
9   at java.lang.reflect.Method.invokeNative(Native Method)
10  at java.lang.reflect.Method.invoke(Method.java:507)
11  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
13  at dalvik.system.NativeStart.main(Native Method)
14  Caused by: java.lang.IllegalStateException: No host
15  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1235)
16  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
17  at android.support.v4.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:2056)
18  at android.support.v4.app.FragmentController.dispatchPause(FragmentController.java:198)
19  at android.support.v4.app.FragmentActivity.onPause(FragmentActivity.java:401)
20  at com.mcruiseon.caregiri.Home.onPause(Home.java:635)
21  at android.app.Activity.performPause(Activity.java:3862)
22  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
23  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2345)
24  ... 12 more
25  java.lang.IllegalStateException: No host
26  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1235)
27  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
28  at android.support.v4.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:2056)
29  at android.support.v4.app.FragmentController.dispatchPause(FragmentController.java:198)
30  at android.support.v4.app.FragmentActivity.onPause(FragmentActivity.java:401)
31  at com.mcruiseon.caregiri.Home.onPause(Home.java:635)
32  at android.app.Activity.performPause(Activity.java:3862)
33  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
34  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2345)
35  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
36  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
37  at android.app.ActivityThread.access$1700(ActivityThread.java:117)
38  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:946)
39  at android.os.Handler.dispatchMessage(Handler.java:99)
40  at android.os.Looper.loop(Looper.java:130)
41  at android.app.ActivityThread.main(ActivityThread.java:3687)
42  at java.lang.reflect.Method.invokeNative(Native Method)
43  at java.lang.reflect.Method.invoke(Method.java:507)
44  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
45  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
46  at dalvik.system.NativeStart.main(Native Method)

修改

大多数答案都谈论onBackStack,但我根本不在乎。例如this 有些人有神秘的答案this

1 个答案:

答案 0 :(得分:0)

由于我通过按后退按钮退出应用程序,我得到一个" No Host"例外。我将以下代码从onCreate移到onResume

    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
            R.id.locationMap)).getMapAsync(this);