当我使用MapBox库启动Android应用程序时,我得到了异常:
“android.view.InflateException:二进制XML文件行#9:错误 inflating类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, / vendor / lib,/ system / lib]]
请帮助
这是我的代码:
的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>
答案 0 :(得分:2)
您的MapView XML必须为com.mapbox.mapboxsdk.maps.MapView
而不是com.mapbox.mapboxsdk.views.MapView
使用最新的Mapbox Android SDK版本时可能会有所帮助:
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:4.0.0-rc.1@aar'){
transitive=true
}
确保包含所有必需的权限以及遥测服务:
<service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService" />
要在4.0.0中控制MapView,有一个名为getMapAsync
的新方法,它会在地图准备就绪时进行侦听。一旦它,您可以添加标记,更改相机位置等。所以您的onCreate
看起来像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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!
}
...
最后,请确保在活动生命周期中包含所有mapView方法。它看起来像这样:
// Activity lifecycle calls
@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);
}
希望这有帮助!
答案 1 :(得分:2)
我也遇到了这个问题,但是当将此库添加到我的dependencies
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.23.0'
给予这些证据
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.mapbox.mapboxsdk.maps.MapView
即使我不使用导航!!然后我意识到我必须在setContentView
所以就这样移动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//first initialize Mapbox
Mapbox.getInstance(this, YOUR_MAPBOX_KEY);
//then
setContentView(R.layout.activity_main);
}
我的问题解决了