ThinkGeo.com    |     Blog    |     Wiki    |     Support

IllegalStateException when navigating back to MapView from another fragment

Hi,

We have an application, that has a MapView in a fragment. If i open another fragment on top of that and press the back button on that fragment to navigate back to the map, the application crashes with the following exception:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:5042) at android.view.ViewGroup.addView(ViewGroup.java:4873) at android.view.ViewGroup.addView(ViewGroup.java:4845) at thinkgeo.mapsuite.android.MapView.n_onSizeChanged(Native Method) at thinkgeo.mapsuite.android.MapView.onSizeChanged(MapView.java:63) at android.view.View.sizeChange(View.java:20891) at android.view.View.setFrame(View.java:20852) at android.view.View.layout(View.java:20712) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656) at android.widget.LinearLayout.onLayout(LinearLayout.java:1565) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656) at android.widget.LinearLayout.onLayout(LinearLayout.java:1565) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:903) at android.view.View.layout(View.java:20715) at android.view.ViewGroup.layout(ViewGroup.java:6202) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2802) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2329) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1469) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7209) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1050) at android.view.Choreographer.doCallbacks(Choreographer.java:862) at android.view.Choreographer.doFrame(Choreographer.java:797) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1036) at android.os.Handler.handleCallback(Handler.java:874) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:198) at android.app.ActivityThread.main(ActivityThread.java:6729) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Hi Mikko,

Based your exception message:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.

I think you added MapView again when press back button, but you didn’t remove the first MapView. There are two options for you:

  1. Don’t remove the MapView when you go to another fragment, and no need add MapView again when back to first fragment.

  2. Remove the MapView and add back when press back button.

If you still meet any issue for that, could you please give us one simple for this issue? we will help to check that.

Thanks
Mark

Hi Mark,

I attached a sample: MapViewCrash.zip (99.6 KB)

I don’t manually create or destroy the MapView. Instead the MapView is in a xml layout file, that is inflated on OnCreateView method of the fragment.

The crash seems to be related to the LayerOverlays. If i didn’t add any overlays to the MapView, the sample didn’t crash, but when i added the “mapView.Overlays.Add(overlay)” line to MapFragment.OnViewCreated, the crash happends. I also tried to clear the overlays on MapFragment.OnStop, but that didn’t seem to help.

Hi Mikko,

We will check this issue, if there is any update, we will update to you here.

Thanks
Mark

Hi Mikko,

Our developer viewed your project, and find the reason is how you initialize the LayerOverlay, and it won’t get disposed follow MapFragment.

Please view our changed project and it should works.

MapViewCrash_20191011.zip (97.2 KB)

Wish that’s helpful.

Regards,

Ethan

That did fix the problem. Thank you.

Hi Mikko,

Thanks for your update.

Any question please let us know.

Regards,

Ethan