Al Lelopath Al Lelopath - 1 month ago 8
Android Question

com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException in onCreateView

I have very similar code to the below that works in

AppCompatActivity.onCreate()
. I'm trying to do similarly in a Fragment instead of an Activity. In
Fragment.onCreateView()
however I get the InvalidAccessTokenException on the line indicated below. The access token is generated from the Mapbox website and the String MAPBOX_TOKEN is assigned its value. What am I doing wrong?

@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>

Answer

Your inflating the view before calling MapboxAccountManager. It either needs to go before anything Mapbox related is done in code (including in XML) or a better option would be to add it to your application object like done in our testapp.

Comments