我的代码与AppCompatActivity.onCreate()
中的代码非常相似。我试图在片段而不是活动中做类似的事情。但是在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>
答案 0 :(得分:1)
在致电MapboxAccountManager
之前,您要对视图进行充气。它要么需要在代码中完成任何与Mapbox相关的操作(包括在XML中),要么更好的选择是在我们的testapp中完成add it to your application object。