Search code examples
androidgoogle-mapsandroid-fragmentsandroid-studiogoogle-maps-api-2

android.view.InflateException: Binary XML file line #1: Error inflating class fragment GOOGLE MAPS


I simply created an activity using Google Maps activity template and put my API key and when I run it I am getting this error

(android.view.InflateException: Binary XML file line #1: Error inflating class fragment ).

This is my xml file:

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

This is my activity:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
    @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

Solution

  • Couple days ago I had similar problem. I had this problem because I upgrade my android SDK and target sdk is 23.
    Google changed sdk and something around Fragment. When you will look at your xml file, you will see that problem is in line number 1- probably <fragment> attribute.
    Ok and here an answer: Try it:
    Implement interface OnMalReadyCallback:

    public class WorkPlaceRegisterActivity extends FragmentActivity implements OnMapReadyCallback{
    

    in onCreate() use this:

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_map_form_registration);
    
        **SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.workMapRegistration);
        mapFragment.getMapAsync(this);**
    

    next add onMapReady overrided method:

     @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
    
        Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
    

    I hope that I'm not missing anything, I will check later hoply help.

    Try secrete fragment in activity xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/workMapRegistration"
        tools:context="com.mytway.activity.registerformactivity.WorkPlaceRegisterActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        tools:layout="@layout/abc_action_bar_title_item" />
    
    
    
    </RelativeLayout>
    

    And my depen dependencies in build.gradle file:

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/Parse-1.10.3/Parse-1.10.3.jar')
    compile files('libs/Parse-1.10.3/bolts-android-1.2.1.jar')
    compile files('libs/Parse-1.10.3/ParseCrashReporting-1.10.3.jar')
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.android.support:appcompat-v7:23.1.0'
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.9.5"
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    

    }