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)
CodePudding user response:
you simply can't be shure that onMapReady
will be called before Activity
s 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
CodePudding user response:
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 OKEY");
}
});