简体   繁体   中英

Error inflating class fragment Google Maps - Java

I am getting an error when trying to inflate my map, but I have copied it straight from a working example

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    <fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

And then in my activity

public class Activity extends FragmentActivity {
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resturant);


    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        }
    }

}

And I get android.view.InflateException: Binary XML file line #64: Error inflating class fragment

Thanks

Check that you import import android.support.v4.app.Fragment;

and in your layout:

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

I suggest to load the map in this way:

FragmentManager fManager= getChildFragmentManager();
mapFragment = (SupportMapFragment) fManager
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    if (googleMap == null) {
                        Log.d("MAPA_FRAGMENT", "Sorry! unable to create maps");
                    } else {
                        mMap = googleMap;
                    }
                }
            });

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