com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException 在 onCreateView 中
com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException in onCreateView
我的代码与下面在 AppCompatActivity.onCreate()
中运行的代码非常相似。我试图在 Fragment 而不是 Activity 中做类似的事情。然而,在 Fragment.onCreateView()
中,我在下面指示的行中得到了 InvalidAccessTokenException。访问令牌是从 the Mapbox website 生成的,字符串 MAPBOX_TOKEN 被分配了它的值。我做错了什么?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.individual, container, false);
<set various TextViews and ImageViews>
MapboxAccountManager.start(getActivity(), MAPBOX_TOKEN);
MapView mapView = (MapView) view.findViewById(R.id.mapView); // create map
mapView.setStyleUrl(Style.LIGHT); // set MapBox streets style
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
<set various things on mapboxMap>
}
});
mapView.setClickable(true);
mapView.onCreate(savedInstanceState); // com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException:
return view;
}
individual.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:id="@+id/individual_gridlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:alignmentMode="alignBounds"
android:columnCount="5"
android:columnOrderPreserved="false">
<!-- column 0 -->
<ImageView
android:id="@+id/divison_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_column="0"
android:layout_row="0"
android:layout_rowSpan="4"
android:src="@mipmap/ic_launcher"
android:background="@null"/>
<!-- some TextViews here -->
<!-- column 4 -->
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="0"
android:layout_column="4"
mapbox:style_url="mapbox://styles/mapbox/streets-v9"
mapbox:center_latitude="30.1"
mapbox:center_longitude="-83.5"
mapbox:zoom="11"/>
</GridLayout>
您在调用 MapboxAccountManager
之前膨胀视图。它要么需要在代码中完成与 Mapbox 相关的任何事情之前(包括在 XML 中),要么更好的选择是 add it to your application object 就像在我们的 testapp 中所做的那样。
我的代码与下面在 AppCompatActivity.onCreate()
中运行的代码非常相似。我试图在 Fragment 而不是 Activity 中做类似的事情。然而,在 Fragment.onCreateView()
中,我在下面指示的行中得到了 InvalidAccessTokenException。访问令牌是从 the Mapbox website 生成的,字符串 MAPBOX_TOKEN 被分配了它的值。我做错了什么?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.individual, container, false);
<set various TextViews and ImageViews>
MapboxAccountManager.start(getActivity(), MAPBOX_TOKEN);
MapView mapView = (MapView) view.findViewById(R.id.mapView); // create map
mapView.setStyleUrl(Style.LIGHT); // set MapBox streets style
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
<set various things on mapboxMap>
}
});
mapView.setClickable(true);
mapView.onCreate(savedInstanceState); // com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException:
return view;
}
individual.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:id="@+id/individual_gridlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:alignmentMode="alignBounds"
android:columnCount="5"
android:columnOrderPreserved="false">
<!-- column 0 -->
<ImageView
android:id="@+id/divison_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_column="0"
android:layout_row="0"
android:layout_rowSpan="4"
android:src="@mipmap/ic_launcher"
android:background="@null"/>
<!-- some TextViews here -->
<!-- column 4 -->
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="0"
android:layout_column="4"
mapbox:style_url="mapbox://styles/mapbox/streets-v9"
mapbox:center_latitude="30.1"
mapbox:center_longitude="-83.5"
mapbox:zoom="11"/>
</GridLayout>
您在调用 MapboxAccountManager
之前膨胀视图。它要么需要在代码中完成与 Mapbox 相关的任何事情之前(包括在 XML 中),要么更好的选择是 add it to your application object 就像在我们的 testapp 中所做的那样。