我正在使用一个地图活动,它有一个托管地图的片段。
所有这些都在一个选项卡内,如下所示
现在每次我在地图选项卡中并转到详细信息选项卡并返回时,我都会收到以下错误。
12-04 06:05:37.858: E/AndroidRuntime(20034): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
PlaceActivity.java
public class PlaceActivity extends SherlockFragmentActivity implements
OnShareTargetSelectedListener {
private static final String MAPS_KEY = "0rQAS47Cicu2t78I-QQ";
FragmentManager fm = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
MapView mMapView = new MapView(PlaceActivity.this, MAPS_KEY);
mMapView.setClickable(true);
Exchanger.mMapView = mMapView;
//setContentView(R.layout.activity_place);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabDetails = actionBar.newTab();
ActionBar.Tab tabMaps = actionBar.newTab();
tabDetails.setText("Details");
tabMaps.setText("Maps");
tabDetails.setTabListener(new MyTabListener());
tabMaps.setTabListener(new MyTabListener());
actionBar.addTab(tabDetails);
actionBar.addTab(tabMaps);
}
private class MyTabListener implements ActionBar.TabListener {
@Override
public void onTabSelected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
if (tab.getPosition() == 0) {
DetailsFragment fragment = new DetailsFragment();
ft.replace(android.R.id.content, fragment);
} else {
fm.beginTransaction().add(android.R.id.content, MapSample.GeoGigMapFragment.newInstance()).commit();
//MapsFragment fragment = new MapsFragment();
//ft.replace(android.R.id.content, fragment);
//ft.commit();
}
}
@Override
public void onTabUnselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
.
.
.
.
.
}
我的地图活动
MapSample.java
public class MapSample extends MapActivity {
MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public static final class GeoGigMapFragment extends Fragment
{
static GeoGigMapFragment newInstance()
{
GeoGigMapFragment map = new GeoGigMapFragment();
return map;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
MapView mMapView = Exchanger.mMapView;
return mMapView;
}
}
@Override
public void onDestroy() {
super.onDestroy();
// ((ViewGroup)mMapView.getParent()).removeView(mMapView);
// mMapView.removeView(mMapView);
// mMapView.removeAllViews();
// ViewParent som = mMapView.getParent();
// ((ViewGroup) som).removeView(mMapView);
if (mMapView != null)
{
// NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout)
// mMapView.getParent();
// parentView.removeView(mMapView);
((ViewGroup) mMapView.getParent()).removeAllViews();
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
我想要实现的是每次单击 DetailsTab 时我都想销毁 MapSample 或 Map View,以免出现错误。
完整的错误跟踪
12-04 06:26:38.808: E/AndroidRuntime(22066): FATAL EXCEPTION: main
12-04 06:26:38.808: E/AndroidRuntime(22066): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.view.ViewGroup.addView(ViewGroup.java:3210)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.view.ViewGroup.addView(ViewGroup.java:3155)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.view.ViewGroup.addView(ViewGroup.java:3131)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.support.v4.app.NoSaveStateFrameLayout.wrap(Unknown Source)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.support.v4.app.BackStackRecord.run(Unknown Source)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.os.Handler.handleCallback(Handler.java:725)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.os.Handler.dispatchMessage(Handler.java:92)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.os.Looper.loop(Looper.java:137)
12-04 06:26:38.808: E/AndroidRuntime(22066): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-04 06:26:38.808: E/AndroidRuntime(22066): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 06:26:38.808: E/AndroidRuntime(22066): at java.lang.reflect.Method.invoke(Method.java:511)
12-04 06:26:38.808: E/AndroidRuntime(22066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-04 06:26:38.808: E/AndroidRuntime(22066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-04 06:26:38.808: E/AndroidRuntime(22066): at dalvik.system.NativeStart.main(Native Method)