Android MapBox 膨胀错误。没有找到类 MapView

Bad*_*ene 3 android inflate-exception mapbox

当我使用 MapBox 库启动 android 应用程序时,出现异常:

“android.view.InflateException:二进制 XML 文件第 9 行:错误膨胀类 com.mapbox.mapboxsdk.views.MapView”

字段“原因”包含以下文本:

java.lang.ClassNotFoundException:在路径上找不到类“com.mapbox.mapboxsdk.views.MapView”:DexPathList[[zip 文件“/data/app/com.example.my.mymapbox-2/base.apk” ],nativeLibraryDirectories=[/data/app/com.example.my.mymapbox-2/lib/arm, /v​​endor/lib, /system/lib]]

请帮忙

这是我的代码:

构建.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
}
}
Run Code Online (Sandbox Code Playgroud)

主活动.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);
}
Run Code Online (Sandbox Code Playgroud)

}

活动_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>
Run Code Online (Sandbox Code Playgroud)

Rad*_*esh 7

我也遇到了这个问题,但是当将此库添加到我的 dependencies

implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.23.0'
Run Code Online (Sandbox Code Playgroud)

给出这个例外

Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.mapbox.mapboxsdk.maps.MapView
Run Code Online (Sandbox Code Playgroud)

即使我不使用导航!然后我意识到我必须先初始化 MapboxsetContentView

所以就这样向上移动

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

我的问题解决了

  • 在 setContentView 之前初始化 Mapbox。确切地 (2认同)

cam*_*ace 5

您的 MapView XMLcom.mapbox.mapboxsdk.maps.MapView不需要com.mapbox.mapboxsdk.views.MapView

使用最新的 Mapbox Android SDK 版本时可能有帮助的其他事项:

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

确保包含所有必需的权限以及遥测服务:

    <service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService" />
Run Code Online (Sandbox Code Playgroud)

要控制 8.4.0 中的 MapView,有一个新方法称为 getMapAsync,它在地图准备好时监听。完成后,您可以添加标记、更改相机位置等。

这是请求许可的方法:

    <service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService" />
Run Code Online (Sandbox Code Playgroud)

你的onCreate方法应该是这样的:

Mapbox.getInstance(this, ACCESS_TOKEN);
Run Code Online (Sandbox Code Playgroud)

最后,确保在活动生命周期中包含所有 mapView 方法。它看起来像这样:

    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!

            }

    ... 
Run Code Online (Sandbox Code Playgroud)