2016-06-21 92 views
2

當我在XML文件中膨脹MapView時出現錯誤。 訪問令牌應該是正確的,我還將遠程信息處理包括在清單中。我使用這些版本mapbox 'com.mapbox.mapboxsdk:mapbox-android-sdk:[email protected]'當在佈局文件中膨脹MapView時出錯

這裏是我的佈局檔。

<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 

回答

0

@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:[email protected]'){ 
    transitive=true 
} 

以下是您的應用在波茨坦上的Mapbox地圖示例。

Mapbox Map

+0

我添加的軟件包名稱也改變了工具:?上下文「.Maps.MapMapboxActivity」但第二個沒有解決的問題是什麼意思與‘你類的名稱 - ’謝謝 – MTwain

+0

@twain,我完全改變了我的答案,請接受,如果這是正確的。 – RobLabs

+0

非常感謝!真的很酷......我是真正的新和SO和github,但現在我知道了巨大的優勢。 – MTwain

0

我認爲這可能是內存的問題。我在誇大複雜觀點方面遇到了類似的問題。儘量減少地圖的寬度和高度。

+0

不是。我減少了地圖對象,但它不起作用,並顯示相同的錯誤。 :( – MTwain

-1

您需要添加

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

應用程序類而不是活動。