0

I am trying to follow This tutorial and I am getting an error This is the stack trace

Process: com.example.mapp, PID: 7624
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapp/com.example.mapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mapbox.mapboxsdk.maps.MapView.onCreate(android.os.Bundle)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mapbox.mapboxsdk.maps.MapView.onCreate(android.os.Bundle)' on a null object reference
        at com.example.mapp.MainActivity.onCreate(MainActivity.java:21)

This the mapview part of activity_main.xml

    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        mapbox:mapbox_cameraTargetLat="37.7747"
        mapbox:mapbox_cameraTargetLng="-122.4443"
        mapbox:mapbox_styleUrl="mapbox://styles/mapbox/streets-v10"
        mapbox:mapbox_cameraZoom="12"
        />

and this is complete MainActivity.Java ( Removed all the imports + few more functions of map box)

public class MainActivity extends AppCompatActivity {

    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Mapbox.getInstance(this, getString(R.string.access_token) );
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }

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

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

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

I tried the following links but they didn't work link 1 link 2 link 4

Thanks in advance.

Krish Yadav
  • 165
  • 11

1 Answers1

0

That YouTube video you're following is a bit old. Setting up a simple MapView is now done differently. For example, you can't set a map style URL via XML anymore.

https://docs.mapbox.com/android/maps/examples/create-a-simple-map-view shows the basic way to set up a Mapbox map.

langsmith
  • 2,529
  • 1
  • 11
  • 13