0

I'm trying to addMarker when I open my fragment so in onMap Ready function I assigned my map and I'm calling map onStart() method but I get null

@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
    map = mapboxMap;
}

@Override
public void onStart() {
    super.onStart();
    mapView.onStart();
    Bundle args = getArguments();
    if (args != null) {
        System.out.println("MY PATROL => "+args);
        mapboxAddMarker(args.getString("QRResult"));
    }
}

public void mapboxAddMarker(String coords){
    String[] coordArr =coords.split(" ");

    LatLng coord = new LatLng();
    coord.setLatitude(Double.parseDouble(coordArr[0]));
    coord.setLongitude(Double.parseDouble(coordArr[1]));

    map.addMarker(new MarkerOptions().position(coord));
}

Also, I know addMarker is deprecated but it's an example I'll fix it.

Lastly My Error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.rtets, PID: 10727
    java.lang.NullPointerException: Attempt to invoke virtual method 'com.mapbox.mapboxsdk.annotations.Marker com.mapbox.mapboxsdk.maps.MapboxMap.addMarker(com.mapbox.mapboxsdk.annotations.MarkerOptions)' on a null object reference
        at com.example.rtets.ui.fragments.MyPatrolFragment.mapboxAddMarker(MyPatrolFragment.java:83)
        at com.example.rtets.ui.fragments.MyPatrolFragment.onViewCreated(MyPatrolFragment.java:96)
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8633)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Ramesh R
  • 7,009
  • 4
  • 25
  • 38

2 Answers2

2

Well I solved my problem I implemented my fragment OnMapReadyCallback

and overridden it but I think in the fragment that does not work so I deleted the OnMapReadyCallback and I wrote OnMapReadyCallback in getMapAsync method

mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(@NonNull MapboxMap mapboxMap) {
            System.out.println("MAP IS OKAY");
        }
    });
Komal Kadam
  • 808
  • 1
  • 7
  • 18
1

you simply can't be shure that onMapReady will be called before Activitys lifecycle callback will be called (like onStart or onResume). Map initialization is async operation, thus you have to pass callback for getting information when it is ready (just like method says). you should init your markers in onMapReady

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Maybe I want to add marker after 10 secounds of onMapReady or something like that is there any ways to addMarker or SetStyle out of onMapReady – muhammedlevent Dec 16 '21 at 07:52
  • you can add at any time, also in `onStart`, there is a chance that on some fast devices map will load (`onMapReady` called) before `onStart` and your current code will work. BUT you must be always aware that `map` reference may be `null` as map may be initializing for split of second, few seconds, or even never initialize. so add persistent markers (from bundle) just after proper initialization (`onMapReady` call) and after that you may work with `map` object at any time (as long as `Activity`/`Fragment` is running) – snachmsm Dec 16 '21 at 07:55
  • yeah true words but even if check null control how can I handle the null map situation as a conclusion my map is null and the things that I want to do with map can not be done – muhammedlevent Dec 16 '21 at 07:57
  • well, just don't do "map stuff", instead show some splash informing user that map can't be loaded. you can introduce own timer starting in `onCreate` and firing e.g. 5 secs later checking that map is initialized properly. if not make some UI fallback with proper info, if yes enable map-related features/GUI. markers should be loaded as soon as map loads, so in `onMapReady` – snachmsm Dec 16 '21 at 09:50