Mapbox Application crashes while launching

1.3k views Asked by At

I am trying to create a simple mapbox application which shows a mapview. There is no specific error shown while debugging but the application crashes while launching itself.Here is the .java file which I created.

public class MainActivity extends AppCompatActivity {

private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Mapbox.getInstance(this,String.valueOf(R.string.access_token));

    setContentView(R.layout.activity_main);
    mapView=(MapView)findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);

    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {

        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    mapView.onStart();
}

@Override
protected void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    mapView.onPause();
}
}

Here is my .xml file

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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/activity_main"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
tools:context="com.example.logusubramaniyan.choropleth1.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:mapbox_cameraTilt="20"
    mapbox:mapbox_cameraZoom="12"/>
</LinearLayout>

I don't understand why the activity fails to launch when run on either emulator or any device.Any ideas?

Here is my StackTrace result.

FATAL EXCEPTION: main Process: com.example.logusubramaniyan.choropleth1, PID: 17191 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.logusubramaniyan.choropleth1/com.example.logusubramaniyan.choropleth1.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class com.mapbox.mapboxsdk.maps.MapView at android.app.ActivityThread.performLaunchActivity

4

There are 4 answers

0
Aadhil Ahmed On BEST ANSWER

String.valueOf() doesn't properly convert it to a string value,hence not properly reading the access token.

Try pasting the access token directly instead of giving it is R.string.something.

Your problem should be resolved.

1
Amit Bhandari On

Try putting this line

 Mapbox.getInstance(this,String.valueOf(R.string.access_token));

after setContentView line and check.

5
cab404 On

Try adding access token to view in layout like that: mapbox:access_token="access_token"

0
钟智强 On

To support @aadhil's answer,

Try to adjust this line

String.valueOf(R.string.access_token)

to

this.getResources().getString(R.string.access_token)