【问题标题】:Error when it comes to inflate the MapView in Layout-File在布局文件中为 MapView 充气时出错
【发布时间】:2016-10-23 02:58:27
【问题描述】:

在 XML 文件中对 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.access$800(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

【问题讨论】:

    标签: java android xml android-layout mapbox


    【解决方案1】:

    @twain,谢天谢地,你在 Github 上发布了你的代码。问题的根源是权限。

    这里是Github commit that fixes this up

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

    原因:java.lang.NoClassDefFoundError:解析失败: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 地图示例,该地图来自波茨坦。

    【讨论】:

    • 我添加了包名并将 tools:context 更改为 ".Maps.MapMapboxActivity" 。但是第二个并没有解决问题。 “你的班级的名字——”是什么意思?提前致谢。
    • @twain,我完全改变了我的答案。如果这是正确的,请接受。
    • 非常感谢!真的很酷...我是 SO 和 github 的 realtiv 新手,但现在我知道它们的巨大优势。
    【解决方案2】:

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

    【讨论】:

    • 并非如此。我减少了地图对象,但它不起作用并显示相同的错误。 :(
    【解决方案3】:

    你需要添加

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

    应用程序类中而不是在活动中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多