“ GoogleMapLocation”类:


    public class GoogleMapLocation extends FragmentActivity implements OnMapReadyCallback {
        Location currentLocation;
        FusedLocationProviderClient fusedLocationProviderClient;
        private static final int REQUEST_CODE = 101;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
            fetchLocation();
        }
        private void fetchLocation() {
            if (ActivityCompat.checkSelfPermission(
                    this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                    this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
                return;
            }
            Task<Location> task = fusedLocationProviderClient.getLastLocation();
            task.addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    if (location != null) {
                        currentLocation = location;
                        Toast.makeText(getApplicationContext(), currentLocation.getLatitude() + "" + currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
                        SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMap);
                        assert supportMapFragment != null;
                        supportMapFragment.getMapAsync(GoogleMapLocation.this);
                    }
                }
            });
        }
//...
//...



activity_google_map_location.xml:


<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myMap"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GoogleMapLocation"/>


此行导致问题和崩溃的应用程序:

supportMapFragment.getMapAsync(GoogleMapLocation.this);


错误是:

>     2020-01-16 11:26:10.079 21906-21906/com.example.cryptowallet E/AndroidRuntime: FATAL EXCEPTION: main
>         Process: com.example.cryptowallet, PID: 21906
>         java.lang.NullPointerException: Attempt to invoke virtual method 'void
> com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'
> on a null object reference


依存关系:

implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'


我不确定在这里做错了什么,我怀疑这可能是activity_google_map_location.xml中的问题?

最佳答案

用以下内容替换您的supportMapFragment.getMapAsync(GoogleMapLocation.this);,因为它是FragmentActivity supportMapFragment.getMapAsync(getApplicationContext());

09-04 12:47