Mahbub Iftekhar
Mahbub Iftekhar

Reputation: 17

Binary XML file - error inflating class fragment

I am getting an error saying " Binary XML file line #6: Error inflating class fragment". I tried searching on here and found some solutions but they haven't worked, any suggestions would be greatly appreciated.

I was using this example of Java code to implement this for the app. https://developers.google.com/maps/documentation/android-sdk/start

I am fairly new to Android Programming with Java, I started a year ago using Kotlin and am doing this in Java to help a friend.

Thanks in advance

MapActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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 MapActivity extends FragmentActivity implements OnMapReadyCallback {

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

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

    @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

}

StackTrace:

06-25 16:32:26.142 31234-31234/com.example.miftekhar.androidapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.miftekhar.androidapp, PID: 31234
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miftekhar.androidapp/com.example.jakub.androidapp.MapActivity}: android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:6523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
 Caused by: android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
 Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
    at com.google.maps.api.android.lib6.drd.q.b(:com.google.android.gms.dynamite_dynamitemodulesb@[email protected] (040408-197041431):40)
    at com.google.maps.api.android.lib6.auth.e.a(:com.google.android.gms.dynamite_dynamitemodulesb@[email protected] (040408-197041431):11)
    at com.google.maps.api.android.lib6.impl.e.a(:com.google.android.gms.dynamite_dynamitemodulesb@[email protected] (040408-197041431):4)
    at com.google.android.gms.maps.internal.b.a(:com.google.android.gms.dynamite_dynamitemodulesb@[email protected] (040408-197041431):24)
    at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms.dynamite_dynamitemodulesb@[email protected] (040408-197041431):69)
    at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.dynamite_dynamitemodulesb@[email protected] (040408-197041431):32)
    at com.google.android.gms.maps.internal.i.onTransact(:com.google.android.gms.dynamite_dynamitemodulesb@[email protected] (040408-197041431):31)
    at android.os.Binder.transact(Binder.java:499)
    at com.google.android.gms.internal.maps.zza.transactAndReadException(Unknown Source)
    at com.google.android.gms.maps.internal.zzf.zzc(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment$zzb.zzc(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment$zzb.createDelegate(Unknown Source)
    at com.google.android.gms.dynamic.DeferredLifecycleHelper.zza(Unknown Source)
    at com.google.android.gms.dynamic.DeferredLifecycleHelper.onInflate(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
    at android.support.v4.app.Fragment.onInflate(Fragment.java:1302)
    at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3714)
    at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:114)
    at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
    at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:869)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:832)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:454)
    at com.android.internal.policy.HwPhoneWindow.setContentView(HwPhoneWindow.java:280)
    at android.app.Activity.setContentView(Activity.java:2515)
    at com.example.jakub.androidapp.MapActivity.onCreate(MapActivity.java:16)
    at android.app.Activity.performCreate(Activity.java:6910)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:6523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:screenOrientation="landscape">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    </android.support.constraint.ConstraintLayout>

Gradle:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.miftekhar.androidapp"
        minSdkVersion 17
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.google.android.gms:play-services-location:15.0.1'
    implementation 'com.google.maps.android:android-maps-utils:0.5'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.maps.android:android-maps-utils:0.5'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Upvotes: 0

Views: 530

Answers (2)

Michael Dodd
Michael Dodd

Reputation: 10280

This is the key line of the error

API key not found. Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

So firstly you need to sign up for an API key from the Google Maps Platform website. You then need to add this API key to AndroidManifest.xml inside the <application> tag like so:

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

Where API_KEY is the key you received from the Google Maps Platform website.

Upvotes: 1

Bubu
Bubu

Reputation: 1543

As the log says :

 Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

You need to add the Google API key in your manifest file like :

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

Upvotes: 1

Related Questions