Error inflating class Fragment using maps

Asked

Viewed 153 times

0

When I run my app, after selecting the event that directs to a activity using maps, it ends with Fatal Exception.

luizugliano.com.br.lugaresfavoritos.Mapsactivity}: android.view.Inflateexception: Binary XML file line #12: Binary XML file line #12: Error inflating class Fragment

I’m using the API23 and performed the update for support SupportMapFragment.

activity_maps

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

</LinearLayout>

build.Gradle dependencies

compile 'com.google.android.gms:play-services:6.+'

Mapsactivity (I just created a new map app by Android Studio itself)

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

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);

    Intent intent = getIntent();
    Log.i("InfoLocalizacao", Integer.toString(intent.getIntExtra("InfoLocalizacao", -1)));

}

@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));
}
}
  • The layout that has the map is the one that posted or beyond the Fragment has more views?

  • @ramaral the app has two layouts but only the one I posted has the map.

  • Change the gsm Compile from: compile 'com.google.android.gms:play-services:6.+' for: compile 'com.google.android.gms:play-services:8.1.0'. And add your manifest file.

1 answer

1


If the layout contains only the map does not need to include it in a Linearlayout.

The activity_maps.xml it will be worth it:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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" />

If the map is to be included in a layout with other views you have to remove lines with xmlns:..., they’re already in the Linearlayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <fragment 
            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" />

</LinearLayout>
  • Keeps making the same mistake =[

  • Used with or without Linearlayout?

  • I tried both ways. I also thought about the possibility of using the getChildFragmentManager() in charge SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()&#xA; .findFragmentById(R.id.map); but gave did not recognize the getChildFragmentManager(). It must be because of extends FragmentActivity

  • The only thing I saw that could be wrong was the issue of lines xmlns:, otherwise the code is a copy of what is in Google Maps Android API - Getting Started

  • Exactly. Anyway, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.