Getting " Caused by: java.lang.NullPointerException: Listener must not be null" when using fusedLocationProviderClient.requestLocationUpdates

aryan saraf :

This is the second activity of the app. I have already asked for permission to access location in the first activity.This is the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.livelocationfinder/com.example.livelocationfinder.MapsActivity}: java.lang.NullPointerException: Listener must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3123)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7099)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.NullPointerException: Listener must not be null
        at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:11)
        at com.google.android.gms.common.api.internal.ListenerHolders.createListenerHolder(Unknown Source:13)
        at com.google.android.gms.location.FusedLocationProviderClient.requestLocationUpdates(Unknown Source:14)
        at com.example.livelocationfinder.MapsActivity.onCreate(MapsActivity.java:54)
        at android.app.Activity.performCreate(Activity.java:7327)
        at android.app.Activity.performCreate(Activity.java:7318)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7099) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 
2020-04-01 22:44:27.324 30588-30588/com.example.livelocationfinder I/Process: Sending signal. PID: 30588 SIG: 9

This is the code of the onCreate() method and I am getting the error in the last line. The locationCallback has been initialized in onMapReady() method.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MapsActivity.this);
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(5000);
    fusedLocationProviderClient.requestLocationUpdates(locationRequest,locationCallback,Looper.getMainLooper());
Cauê Jannini :

Are you initializing your locationCallback anywhere in your code? You should initialize this object before passing it to "requestLocationUpdates". For example, like this:

    locationCallback = new LocationCallback(){
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);

                // Do something with locationResult
            }
        };

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=393741&siteId=1