Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

java - Error when using fusedLocationProvider.removeLocationUpdates (locationCallback) Android Studio

I am making a code to capture the user's current location and track the GPS coordinates at the exact moment.

I have 2 buttons that I am using, one is the "RecordButton" button that is used to start recording and at this moment gps tracking is also started.

and the StopButton button, which is used to stop all tracking. Only when the stopButton is pressed, the app still sends the coordinates and does not stop sending.

already tried using

fusedLocationProviderClient.removeLocationUpdates (locationCallback); inside the StopRecordButton Listener.

If anyone can help I appreciate it.

MY CODE:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...