使用 fusedLocationProvider.removeLocationUpdates (locationCallback) Android Studio 时出错
Posted
技术标签:
【中文标题】使用 fusedLocationProvider.removeLocationUpdates (locationCallback) Android Studio 时出错【英文标题】:Error when using fusedLocationProvider.removeLocationUpdates (locationCallback) Android Studio 【发布时间】:2021-04-13 08:37:50 【问题描述】:我正在编写一个代码来捕获用户的当前位置并在准确的时刻跟踪 GPS 坐标。
我正在使用 2 个按钮,一个是用于开始录制的“RecordButton”按钮,此时 gps 跟踪也已启动。
以及用于停止所有跟踪的 StopButton 按钮。只有当stopButton被按下时,app仍然会发送坐标并且不会停止发送。
已经尝试使用
fusedLocationProviderClient.removeLocationUpdates(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();
);
【问题讨论】:
【参考方案1】: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)
【讨论】:
以上是关于使用 fusedLocationProvider.removeLocationUpdates (locationCallback) Android Studio 时出错的主要内容,如果未能解决你的问题,请参考以下文章
FusedLocationProvider - 如果位置模式为“仅设备”,任何获取位置的方法?
FusedLocationProvider 始终返回 null Location 对象
FusedLocationProvider 有时会给出错误的位置
HMS FusedLocationProvider 和 HuaweiMap 不工作
从 FusedLocationProvider(Google Play 服务)获取位置不会给出低于 10 米的准确度
使用 Google Play 服务我可以在离线模式下获取最新位置吗(FusedLocationProvider Api)