首页 > 解决方案 > 地图上的标记 - 尝试在空对象引用上调用虚拟方法

问题描述

“谷歌地图位置”类:

    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 中的问题?

标签: javaandroid-studio

解决方案


用以下替换你的supportMapFragment.getMapAsync(GoogleMapLocation.this);,因为它是 FragmentActivitysupportMapFragment.getMapAsync(getApplicationContext());


推荐阅读