简体   繁体   中英

Android IllegalStateException with google maps api v2

I have an application and inside it I have to show a fragment (from v4 suppport library) with a map and a location inside it. I used the play services library, and I created and XML containing a support map fragment

<fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

I use in my application multiple fragments and a FrameLayout in which I make fragment transactions.

I have defined the location fragment like this:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class LocationFragment extends Fragment{
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    RelativeLayout rl;
    private GoogleMap map=null;
    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceBundle)
    {
        if (map==null){
            rl =  (RelativeLayout) inflater.inflate(R.layout.maps, container,false);
            map  = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                    .title("Hamburg"));
            Marker kiel = map.addMarker(new MarkerOptions()
                    .position(KIEL)
                    .title("Pilates")
                    .snippet("Pilates teaching")
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.ic_launcher)));
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        }
        return rl;
    }

}

Everything works ok when I click for the first time on the button that activates the location fragment. The problem appear when I click on another fragment and then on this one again, I get the following errors:

E/AndroidRuntime(30571): FATAL EXCEPTION: main
E/AndroidRuntime(30571): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime(30571):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3620)
E/AndroidRuntime(30571):    at android.view.ViewGroup.addView(ViewGroup.java:3491)
E/AndroidRuntime(30571):    at android.view.ViewGroup.addView(ViewGroup.java:3436)
E/AndroidRuntime(30571):    at android.view.ViewGroup.addView(ViewGroup.java:3412)
E/AndroidRuntime(30571):    at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
E/AndroidRuntime(30571):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
E/AndroidRuntime(30571):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
E/AndroidRuntime(30571):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime(30571):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
E/AndroidRuntime(30571):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
E/AndroidRuntime(30571):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(30571):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(30571):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(30571):    at android.app.ActivityThread.main(ActivityThread.java:4921)
E/AndroidRuntime(30571):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(30571):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(30571):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
E/AndroidRuntime(30571):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
E/AndroidRuntime(30571):    at dalvik.system.NativeStart.main(Native Method)
E/android.os.Debug( 2143): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

can you please help me with this?

尝试这个:

rl =  (RelativeLayout) inflater.inflate(R.layout.maps, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM