Android MapBox inflating error. Didn't find class MapView
Asked Answered
B

2

5

When I launch android app with MapBox library I get exception:

"android.view.InflateException: Binary XML file line #9: Error inflating class com.mapbox.mapboxsdk.views.MapView"

Field "cause" contains this text:

java.lang.ClassNotFoundException: Didn't find class "com.mapbox.mapboxsdk.views.MapView" on path: DexPathList[[zip file "/data/app/com.example.my.mymapbox-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.my.mymapbox-2/lib/arm, /vendor/lib, /system/lib]]

Help please

This is my code:

build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.example.my.mymapbox"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:4.0.0-beta.2@aar'){
    transitive=true
}
}

MainActivity.java

package com.example.my.mymapbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.mapbox.mapboxsdk.views.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    mapbox:access_token="@string/accessToken"/>
<!-- note the access token string created in the previous step -->
</RelativeLayout>
Bacolod answered 28/3, 2016 at 13:44 Comment(3)
Did you try a full clean & gradle build of your project?Zandrazandt
Clean & gradle build didn't help.Bacolod
Another think that you should keep in mind is that an invalid Token will cause the also an inflating error.Xeniaxeno
C
8

Your XML for the MapView needs to be com.mapbox.mapboxsdk.maps.MapView not com.mapbox.mapboxsdk.views.MapView

Other things that might help when using the latest Mapbox Android SDK version which is:

    implementation ('com.mapbox.mapboxsdk:mapbox-android-sdk:8.4.0@aar'){
        transitive=true
    }

make sure to include all the required permissions as well as Telemetry service:

    <service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService" />

To control the MapView in 8.4.0 there is a new method called getMapAsync which listens for when the map is ready. Once it is, you can add markers, change the camera position, etc.

This is how to ask for the permission:

Mapbox.getInstance(this, ACCESS_TOKEN);

Your onCreate method should look something like this:

    String ACCESS_TOKEN = "ACCESS_TOKEN_HERE"

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Be sure to call this setContentView
        Mapbox.getInstance(this, ACCESS_TOKEN);

        setContentView(R.layout.activity_main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);

        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {

                // add markers, change camera position, etc. here!

            }

    ... 

Lastly, make sure you include all the mapView methods within your activities lifecycle. It will look like this:

    // Activity lifecycle methods
    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
Claudianus answered 28/3, 2016 at 15:17 Comment(10)
Yes! It helps, Thank you:) Now I have another exception:DBacolod
I edited my answer with some useful steps. If you post your logcat where the exception occurs I can further help you out.Claudianus
May be I should create other topic. Next exception message is: "/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)" This is logcat pastebin.com/jtYivuzVBacolod
Probably best to create a new question for this one. Make sure to include your logcat and what device your trying it on, along with any other relevant info and i'll be able to help. If I resolved your issue mentioned within your question above, please mark this as answered so others with the same question can quickly find the answer.Claudianus
Some report. When I change "com.mapbox.mapboxsdk.views.MapView" to "com.mapbox.mapboxsdk.maps.MapView". The "InflateException" has gone, and I get EGL_BAD_DISPLAY exception. After I add "telemetry service" permission (other permissions already have been added, sorry for poor english) and add mapView methods to activity life cycle, EGL_BAD_DISPLAY exception has gone too.Bacolod
Excellent, glad I was able to help!Claudianus
Cammance, I see in you profile that you related to MapBox, so may be it information will be usefull. The code examples that I use with incorrect "com.mapbox.mapboxsdk.views.MapView" are placed there: mapbox.com/help/first-steps-android-sdk mapbox.com/android-sdk/#supported_android_versionsBacolod
We are aware that most examples are for the 3.2.0 Android SDK still. Since 4.0.0 is still in beta and it hasn't been officially released we haven't updated the examples yet. Thanks for informing us and I hope this didn't cause too much confusion. Also since you're new to Stack Overflow, to mark an answer correct you just click the check mark below the upvote/downvote arrows. More info can be found here if you are having trouble.Claudianus
Just wanted to give you a heads up that we have updated our help guides and examples since we released version 4.0.0 today. You can read up on some of the changes in the blog post. Hope this helps and once again, sorry for the confusion.Claudianus
Hey, just a heads up in case anyone comes looking for help: in Mapbox 5.0 it needs to be <service android:name="com.mapbox.services.android.telemetry.service.TelemetryService" /> instead of android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService"/>Tshombe
B
10

I also faced this problem but when add this library to my dependencies

implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.23.0'

Give this exeptions

Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.mapbox.mapboxsdk.maps.MapView

Even if i do not use navigation!! Then i realize i must initialize Mapbox before setContentView

So just move up like this

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //first initialize Mapbox
    Mapbox.getInstance(this, YOUR_MAPBOX_KEY);
    //then 
    setContentView(R.layout.activity_main);
}

And my problem solved

Boughton answered 19/11, 2018 at 12:41 Comment(2)
initialize Mapbox before setContentView. ExactlyCerys
just write this line in your Application class' onCreate() method Mapbox.getInstance(this, YOUR_MAPBOX_KEY);Margetmargette
C
8

Your XML for the MapView needs to be com.mapbox.mapboxsdk.maps.MapView not com.mapbox.mapboxsdk.views.MapView

Other things that might help when using the latest Mapbox Android SDK version which is:

    implementation ('com.mapbox.mapboxsdk:mapbox-android-sdk:8.4.0@aar'){
        transitive=true
    }

make sure to include all the required permissions as well as Telemetry service:

    <service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService" />

To control the MapView in 8.4.0 there is a new method called getMapAsync which listens for when the map is ready. Once it is, you can add markers, change the camera position, etc.

This is how to ask for the permission:

Mapbox.getInstance(this, ACCESS_TOKEN);

Your onCreate method should look something like this:

    String ACCESS_TOKEN = "ACCESS_TOKEN_HERE"

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Be sure to call this setContentView
        Mapbox.getInstance(this, ACCESS_TOKEN);

        setContentView(R.layout.activity_main);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);

        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {

                // add markers, change camera position, etc. here!

            }

    ... 

Lastly, make sure you include all the mapView methods within your activities lifecycle. It will look like this:

    // Activity lifecycle methods
    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
Claudianus answered 28/3, 2016 at 15:17 Comment(10)
Yes! It helps, Thank you:) Now I have another exception:DBacolod
I edited my answer with some useful steps. If you post your logcat where the exception occurs I can further help you out.Claudianus
May be I should create other topic. Next exception message is: "/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)" This is logcat pastebin.com/jtYivuzVBacolod
Probably best to create a new question for this one. Make sure to include your logcat and what device your trying it on, along with any other relevant info and i'll be able to help. If I resolved your issue mentioned within your question above, please mark this as answered so others with the same question can quickly find the answer.Claudianus
Some report. When I change "com.mapbox.mapboxsdk.views.MapView" to "com.mapbox.mapboxsdk.maps.MapView". The "InflateException" has gone, and I get EGL_BAD_DISPLAY exception. After I add "telemetry service" permission (other permissions already have been added, sorry for poor english) and add mapView methods to activity life cycle, EGL_BAD_DISPLAY exception has gone too.Bacolod
Excellent, glad I was able to help!Claudianus
Cammance, I see in you profile that you related to MapBox, so may be it information will be usefull. The code examples that I use with incorrect "com.mapbox.mapboxsdk.views.MapView" are placed there: mapbox.com/help/first-steps-android-sdk mapbox.com/android-sdk/#supported_android_versionsBacolod
We are aware that most examples are for the 3.2.0 Android SDK still. Since 4.0.0 is still in beta and it hasn't been officially released we haven't updated the examples yet. Thanks for informing us and I hope this didn't cause too much confusion. Also since you're new to Stack Overflow, to mark an answer correct you just click the check mark below the upvote/downvote arrows. More info can be found here if you are having trouble.Claudianus
Just wanted to give you a heads up that we have updated our help guides and examples since we released version 4.0.0 today. You can read up on some of the changes in the blog post. Hope this helps and once again, sorry for the confusion.Claudianus
Hey, just a heads up in case anyone comes looking for help: in Mapbox 5.0 it needs to be <service android:name="com.mapbox.services.android.telemetry.service.TelemetryService" /> instead of android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService"/>Tshombe

© 2022 - 2024 — McMap. All rights reserved.