Krakusekpl Krakusekpl - 4 months ago 24
Android Question

"Error inflating class fragment" when trying to add a map v2 fragment to activity_main

When I add map fragment:

<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=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>


to activity_main.xml I get that error:

FATAL EXCEPTION: main
Process: com.example.koba.ogloszenia, PID: 3073
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.koba.ogloszenia/com.example.koba.ogloszenia.MainActivity}: android.view.InflateException: Binary XML file line #120: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
...


My MainActivity.java:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

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

SupportMapFragment MapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
MapFragment.getMapAsync(this);
}

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


My AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.koba.ogloszenia">

<uses-permission android:name="android.Manifest.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.Manifest.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.Manifest.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY_API_KEY"/>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>



I have included in depedencies


  • com.google.android.gms:play-services:9.2.1
    and

  • com.google.android.gms:play-services-maps:9.2.1



I also have checked "Google Play services" in SDK Tools.

I also looked similar posts but that didn't solve my problem.

Thank you in advance.

Answer

The <meta-data> elements must be within the <application> element of your AndroidManifest.xml but out of the <activity> element.

Comments