使用fusedlocationprovider.RemovelocationUpdate(locationcallback)android studio时出错

g0czyy6m  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(405)

我正在制作一个代码来捕捉用户的当前位置,并在准确的时刻跟踪gps坐标。
我有两个按钮,我正在使用,一个是“recordbutton”按钮,用于开始录制,在这一刻gps跟踪也开始了。
以及停止按钮,用于停止所有跟踪。只有当按下停止按钮时,应用程序仍会发送坐标并且不会停止发送。
已尝试使用
fusedlocationproviderclient.RemovelocationUpdate(locationcallback);在stoprecordbutton侦听器中。
如果有人能帮我,我很感激。
我的代码:

cameraActionButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {   //Botão de Gravação do App; (Record button Action)

                    final HHConfigTCPClient hhudpClient = new HHConfigTCPClient(prefs.getString("prefServerIp", ""),
                            8085);

                    final boolean _paused = state.isPaused();

                    switch (cameraAction) {
                        case RECORD:
                            //Uma thread para rodar a localização
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    MjpegFragment.this.getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {

                                            fusedLocationProviderClient = new FusedLocationProviderClient(getActivity());
                                            locationRequest = new LocationRequest();
                                            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                                            locationRequest.setFastestInterval(4000);
                                            locationRequest.setInterval(2000);
                                            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
                                                    != PackageManager.PERMISSION_GRANTED &&
                                                    ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)
                                                            != PackageManager.PERMISSION_GRANTED) {
                                                return;
                                            }
                                            fusedLocationProviderClient.requestLocationUpdates(locationRequest, new LocationCallback() {
                                                @Override
                                                public void onLocationResult(final LocationResult locationResult) {
                                                    super.onLocationResult(locationResult);
                                                    t.scheduleAtFixedRate(mTimerTask = new TimerTask() {
                                                        public void run() {
                                                            hhudpClient.set(HHConfigTCPClient.PROPERTY.LATITUDE, locationResult.getLastLocation().getLatitude());
                                                            state.setLatitude(locationResult.getLastLocation().getLatitude());
                                                            hhudpClient.set(HHConfigTCPClient.PROPERTY.LONGITUDE, locationResult.getLastLocation().getLongitude());
                                                            state.setLongitude(locationResult.getLastLocation().getLongitude());
                                                        }
                                                    }, 4000, 2000);
                                                }
                                            }, Looper.getMainLooper());

                                        }
                                    });
                                }

                            }).start(); 

stopRecordButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    final HHConfigTCPClient hhudpClient = new HHConfigTCPClient(prefs.getString("prefServerIp", ""),
                            8085);

                    new Thread(new Runnable() {
                        @Override
                        public void run() {

                            HHConfigTCPClient hhudpClient = new HHConfigTCPClient(prefs.getString("prefServerIp", ""),
                                    8085);

                            final double timestamp = System.currentTimeMillis();

                            if (hhudpClient.set(HHConfigTCPClient.PROPERTY.RECORD, 0.0, timestamp)) {
                                fusedLocationProviderClient.removeLocationUpdates(locationCallback);

                            } else {
                                MjpegFragment.this.getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        showToast("Falha de Comunicação: comando stop.");
                                    }
                                });
                            }

                        }
                    }

                    ).start();
                }
            });
sxissh06

sxissh061#

This error appears on my Android Studio:

E/AndroidRuntime: FATAL EXCEPTION: Thread-7
    Process: br.com.visit.hammerhead2, PID: 15224
    java.lang.NullPointerException: Listener must not be null
        at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:9)
        at com.google.android.gms.common.api.internal.ListenerHolders.createListenerKey(Unknown Source:2)
        at com.google.android.gms.location.FusedLocationProviderClient.removeLocationUpdates(Unknown Source:6)
        at br.com.visit.hammerhead2.fragments.MjpegFragment$6$1.run(MjpegFragment.java:718)
        at java.lang.Thread.run(Thread.java:919)

相关问题