Roma Darvish
Roma Darvish

Reputation: 267

Google maps SupportMapFragment error in Fragment

Old version app use MapView.I changed SupportMapFragment error. I know how to solve this problem.

Process: com.test.app, PID: 7847
android.view.InflateException: Binary XML file line #14: Error inflating class fragment

xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

</FrameLayout>

fragment

public class MyFragment extends Fragment implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_map, null);

    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    buildGoogleApiClient();


    return rootView;
}

build.gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services-location:10.2.0'
    compile 'com.google.android.gms:play-services-maps:10.2.0'
    compile 'se.emilsjolander:stickylistheaders:2.5.2'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.facebook.android:facebook-android-sdk:4.0.0'
//compile 'com.google.firebase:firebase-messaging:10.2.0'


 }

Upvotes: 1

Views: 5793

Answers (4)

Adnan
Adnan

Reputation: 1

Just add to the dependency the following:

implementation 'com.google.android.gms:play-services-maps:17.0.1'

Then click Sync Now.

Upvotes: 0

Roma Darvish
Roma Darvish

Reputation: 267

I solved that problem as this way.

if (view != null) {
   ViewGroup parent = (ViewGroup) view.getParent();
   if (parent != null)
      parent.removeView(view);
}

try {
     view = inflater.inflate(R.layout.fragment_partners_map, container, false);
} catch (InflateException e) {
}

First time I'm check that, is the Fragments parents view exists, then I recreate the parent, else I create it.

Upvotes: 1

Bradley Wilson
Bradley Wilson

Reputation: 1197

This is how I have it set up

XML:

 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_marginTop="5dp"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mapView"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        tools:context=".MainActivity" />

Code:

import android.support.v4.app.Fragment;


SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.mapView);
        mapFragment.getMapAsync(this);

Have your fragment Implement OnMapReadyCallback (e.g. extends Fragment implements OnMapReadyCallback) and copy this method:

@Override
    public void onMapReady(GoogleMap map) {
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.setTrafficEnabled(false);
        map.setIndoorEnabled(false);
        map.setBuildingsEnabled(false);
        map.getUiSettings().setZoomControlsEnabled(true);
        LatLng wotw = new LatLng(getLat(), getLong());
        map.addMarker(new MarkerOptions().position(wotw)
                .title("Walk of the Week"));
        map.moveCamera(CameraUpdateFactory.newLatLng(wotw));
        map.animateCamera(CameraUpdateFactory.zoomTo(15.0f));
    }

Make sure you have your API key in your Manifest too:

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="API_KEY_HERE"/>

and the corresponding permissions:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <permission
        android:name="com.example.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

Upvotes: 1

Parthiv Mistri
Parthiv Mistri

Reputation: 166

public class MapFragment extends Fragment implements OnMapReadyCallback {

SupportMapFragment mapFragment;

@Override
public void onMapReady(GoogleMap googleMap) {
    LatLng sydney = new LatLng(-33.852, 151.211);
    googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.map_fragment, container, false);
    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

}

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

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

Upvotes: 1

Related Questions