从Fragment中删除View

时间:2012-12-04 14:09:16

标签: android android-mapview mapactivity

我正在使用Map Activity,它有一个托管MAPS的Fragment。

所有这些都在一个标签内,如下所示

enter image description here

现在每次我在“地图”选项卡中并转到“详细信息”选项卡并返回时,我收到以下错误。

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)

1 个答案:

答案 0 :(得分:3)

你在哪一行得到错误? 尝试添加

contanier.removeAllViews();
onCreateView方法

中的