在 Layout-File 中膨胀 MapView 时出错

Error when it comes to inflate the MapView in Layout-File

我在 XML-File 中膨胀 MapView 时遇到错误。 访问令牌应该是正确的,我还在清单中包含远程信息处理。我使用这些版本的 mapbox 'com.mapbox.mapboxsdk:mapbox-android-sdk:4.1.0-beta.3@aar'

这是我的布局文件。

<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.maps.MapView
    android:id="@+id/mapviewmapbox"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:access_token="@string/accessToken"
    mapbox:center_latitude="40.73581"
    mapbox:center_longitude="-73.99155"
    mapbox:style_url="mapbox://styles/mapbox/streets-v9"
    mapbox:zoom="11" />

</RelativeLayout>`

和 Java-文件:

package de.example.navdrawemap_2.maptest.Maps;

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

import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

import de.example.navdrawemap_2.maptest.R;

public class MapMapboxActivity extends AppCompatActivity {
private MapView mapView;

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

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

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


        }});
}

      /*
    TextView textViewTitle = (TextView)findViewById(R.id.texttitel);
    TextView textViewTitleBig = (TextView)findViewById(R.id.texttitel_big);
    TextView textViewSnippet = (TextView)findViewById(R.id.textsnippet);
    Intent intentbundleStrings = getIntent();

    if (intentbundleStrings != null) {
                    textViewTitleBig.setText(intentbundleStrings.getStringExtra("title"));
        // textViewTitle.setText(intentbundleStrings.getStringExtra("title"));
        // Titel im Header nachtragen
        textViewSnippet.setText(intentbundleStrings.getStringExtra("snippet"));
    }else{
        textViewTitle.setText(intentbundleStrings.getStringExtra("N.A."));
        textViewSnippet.setText(intentbundleStrings.getStringExtra("N.A."));
    } */




// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
    super.onResume();
    mapView.onResume();
}

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

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

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

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

}`

Logcat 显示如下:

06-21 15:46:06.753 13892-13892/de.example.navdrawemap_2.maptest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: de.example.navdrawemap_2.maptest, PID: 13892
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{de.example.navdrawemap_2.maptest/de.example.navdrawemap_2.maptest.Maps.MapMapboxActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.maps.MapView
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
                                                                                  at android.app.ActivityThread.access0(ActivityThread.java:139)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:136)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5086)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                  at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
                                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                                               Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.mapbox.mapboxsdk.maps.MapView

我认为这可能是内存问题。我在夸大复杂视图时遇到了类似的问题。尽量减少地图的宽度和高度。

@twain,幸好你在 Github 中发布了你的代码。问题的根源是权限。

这里是 Github commit that fixes this up.

我在日志中看到的线索,android 权限源代码的关键行在这里。

Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/mapbox/mapboxsdk/location/LocationServices;

它非常神秘,但我以前 运行 遇到过这个问题,因此它引导我找到了解决方案。

你需要

android.permission.ACCESS_WIFI_STATE

而不是

android.permission.ACCESS_NETWORK_STATE

我也将 Mapbox Android SDK 重置为最新版本,但我不确定这有什么影响。

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

这是您的应用程序在波茨坦上空的 Mapbox 地图示例。

您需要添加

Mapbox.getInstance(getApplicationContext(), "MY_DEFAULT_PUBLIC_API_KEY");

应用程序中 class 而不是在 activity.