使用 fusedLocationProviderClient.requestLocationUpdates 时出现“ Caused by: java.lang.NullPointerException: Listener must not be null”

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

这是应用程序的第二个 activity。我已经在第一个 activity.This 中请求了访问位置的权限,错误是:

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

这是 onCreate() 方法的代码,我在最后一行收到错误。 locationCallback 已在 onMapReady() 方法中初始化。

 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());

您是否在代码中的任何位置初始化 locationCallback?您应该在将此对象传递给 "requestLocationUpdates" 之前对其进行初始化。例如,像这样:

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

                // Do something with locationResult
            }
        };