gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:13:38 27 4
gpt4 key购买 nike

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

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

“原因”字段包含此文本:

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]]

请帮忙

这是我的代码:

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
}
}

主 Activity .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>

最佳答案

MapView 的 XML 需要是 com.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
}

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

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

为了在 8.4.0 中控制 MapView,有一个名为 getMapAsync 的新方法,它会在 map 准备就绪时进行监听。完成后,您可以添加标记、更改相机位置等。

这是请求权限的方法:

Mapbox.getInstance(this, ACCESS_TOKEN);

您的 onCreate 方法应如下所示:

    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!

}

...

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

    // 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);
}

关于Android MapBox 膨胀错误。没有找到类 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36263653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com