Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 5.73 KB  |  hits: 315  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. MapView inside Fragment - specified child already has a parent
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:orientation="vertical" >
  7.  
  8.  
  9.     <FrameLayout
  10.         android:id="@+id/map_container"
  11.         android:layout_width="fill_parent"
  12.         android:layout_height="fill_parent" >
  13.     </FrameLayout>
  14.  
  15. </LinearLayout>
  16.        
  17. public class MapScreenActivity extends FragmentActivity {
  18.     protected Fragment fragment;
  19.     protected MapView mapView;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.single_pane_empty);
  25.  
  26.         if (savedInstanceState == null) {
  27.             fragment = new MapScreenFragment();
  28.  
  29.             getSupportFragmentManager().beginTransaction().add(R.id.root_container, fragment)
  30.                     .commit();
  31.         }
  32.     }
  33.  
  34.     public MapView getMapView() {
  35.         if (mapView == null) {
  36.             mapView = new MapView(this, getResources().getString(R.string.maps_api_key));
  37.         }
  38.  
  39.         return mapView;
  40.     }
  41. }
  42.        
  43. public class MapScreenFragment extends Fragment {
  44.     protected ViewGroup mapContainer;
  45.     protected MapView mapView;
  46.  
  47.     @Override
  48.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
  49.         View root = inflater.inflate(R.layout.map_screen_fragment, container);
  50.         mapContainer = (ViewGroup) root.findViewById(R.id.map_container);
  51.         return root;
  52.     }
  53.  
  54.     @Override
  55.     public void onActivityCreated(Bundle savedInstanceState) {
  56.         super.onActivityCreated(savedInstanceState);
  57.  
  58.         mapView = ((MapScreenActivity) getActivity()).getMapView();
  59.         mapView.setClickable(true);
  60.         mapView.setBuiltInZoomControls(true);
  61.  
  62.         mapContainer.addView(mapView);
  63.  
  64.     }
  65.  
  66.     @Override
  67.     public void onDestroyView() {
  68.         super.onDestroyView();
  69.         mapContainer.removeView(mapView);
  70.     }  
  71.  
  72. }
  73.        
  74. 02-24 18:01:28.139: E/AndroidRuntime(502): FATAL EXCEPTION: main
  75. 02-24 18:01:28.139: E/AndroidRuntime(502): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapfragment/com.example.mapfragment.MapScreenActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  76. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  77. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  78. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  79. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
  80. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.os.Handler.dispatchMessage(Handler.java:99)
  81. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.os.Looper.loop(Looper.java:130)
  82. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.main(ActivityThread.java:3683)
  83. 02-24 18:01:28.139: E/AndroidRuntime(502):  at java.lang.reflect.Method.invokeNative(Native Method)
  84. 02-24 18:01:28.139: E/AndroidRuntime(502):  at java.lang.reflect.Method.invoke(Method.java:507)
  85. 02-24 18:01:28.139: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  86. 02-24 18:01:28.139: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  87. 02-24 18:01:28.139: E/AndroidRuntime(502):  at dalvik.system.NativeStart.main(Native Method)
  88. 02-24 18:01:28.139: E/AndroidRuntime(502): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  89. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
  90. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.view.ViewGroup.addView(ViewGroup.java:1871)
  91. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.view.ViewGroup.addView(ViewGroup.java:1828)
  92. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.view.ViewGroup.addView(ViewGroup.java:1808)
  93. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.NoSaveStateFrameLayout.wrap(Unknown Source)
  94. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
  95. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
  96. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.BackStackRecord.run(Unknown Source)
  97. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
  98. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.FragmentActivity.onStart(Unknown Source)
  99. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
  100. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.Activity.performStart(Activity.java:3791)
  101. 02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620)
  102. 02-24 18:01:28.139: E/AndroidRuntime(502):  ... 11 more
  103.        
  104. mapContainer.removeView(mapView);
  105.        
  106. mapContainer.addView(mapView);
  107.        
  108. if(mapView.getParent() != null) {
  109.     mapContainer.addView(mapView);
  110. }
  111.        
  112. if (mapContainer.getChildAt(0) != null){
  113.         mapContainer.removeViewAt(0);
  114. }
  115.        
  116. mapContainer.addView(mapView);
  117.        
  118. @Override
  119. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  120.     return inflater.inflate(R.id.my_layout, container, false);
  121. }