Android Java Google API 应用程序在允许位置访问时崩溃,但在拒绝位置时没有问题......? [复制]
Posted
技术标签:
【中文标题】Android Java Google API 应用程序在允许位置访问时崩溃,但在拒绝位置时没有问题......? [复制]【英文标题】:Android Java Google API app crashing when allowing location access, but no problem when deny location...? [duplicate] 【发布时间】:2021-12-10 18:45:27 【问题描述】:我在 UDEMY 课程中尝试过这段代码。写得和他一样,他可以访问位置并得到一个祝酒词,而我无法打开具有允许位置的应用程序。 允许定位时,我的应用程序崩溃。 但是当拒绝时,我可以看到地图并且不再崩溃了。
***我的谷歌地图 API 也运行良好。还采取了用户互联网和定位服务。
MapsActivity.java:
package com.project.demo2map;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.project.demo2map.databinding.ActivityMapsBinding;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
private GoogleMap mMap;
private ActivityMapsBinding binding;
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, locationListener);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// 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);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener()
@Override
public void onLocationChanged(@NonNull Location location)
Toast.makeText(MapsActivity.this, location.toString(), Toast.LENGTH_SHORT).show();
;
if (Build.VERSION.SDK_INT < 23)
locationManager.requestLocationUpdates(LocationManager.
GPS_PROVIDER, 0, 0, locationListener);
else
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,
new String[]Manifest.permission.ACCESS_FINE_LOCATION, 1);
else
locationManager.requestLocationUpdates(LocationManager.
GPS_PROVIDER, 0, 0, locationListener);
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
崩溃日志:
2021-10-25 14:59:29.003 25984-25984/com.project.demo2map D/AndroidRuntime: Shutting down VM
2021-10-25 14:59:29.006 25984-25984/com.project.demo2map E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.demo2map, PID: 25984
java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onProviderDisabled(java.lang.String)"
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:365)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:275)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:291)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)
【问题讨论】:
Logcat 结果粘贴在问题中。 覆盖方法 onProviderChanged() 【参考方案1】:重写 onProviderChanged() 方法将解决此问题。
【讨论】:
【参考方案2】:检查是否有任何提供者处于活动状态。
if( locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) ||
locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER))
//start listener to requestUpdates ...
else
//show message, no providers enabled.
【讨论】:
显示未启用任何提供程序。 所以你必须引导用户打开一个。例如用户需要打开手机的位置。否则你当然不能得到位置信息。以上是关于Android Java Google API 应用程序在允许位置访问时崩溃,但在拒绝位置时没有问题......? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
添加大量标记时,Google Maps Android API v2 非常慢
com.android.build.api.transform.TransformException:java.util.zip.ZipException:重复条目:com/google/androi
Android 构建失败 - java.lang.IllegalArgumentException:已添加:Lcom/google/api/client/escape/CharEscapers;
致命异常:com.google.maps.api.android.lib6.gmm6.vector.ct 处的 java.lang.ArrayIndexOutOfBoundsException [重复