java - 地图上的标记 - 尝试在空对象引用上调用虚拟方法
问题描述
“谷歌地图位置”类:
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);
,因为它是 FragmentActivitysupportMapFragment.getMapAsync(getApplicationContext());
推荐阅读
- angular - providedIn: 'Root' 与在应用程序组件中使用服务作为提供者有什么区别?
- linear-algebra - 如何线性化暗示?
- javascript - Sequelize 原始查询返回重复数据
- javascript - 在 iFrame 中运行页面时出现 JSON 语法错误
- java - 从 SpringBoot 应用程序上传到私有 S3 存储桶
- java - 如何正确同步类之间的向量以避免使用静态?
- python - 在 Python 中使用 cx_Oracle 从 oracle DB 调用存储函数
- javascript - 使用 Javascript 向 ProBasketballAPI.com 发出 POST 请求
- javascript - 使用 Webpack 解析 css 背景图像 url
- asynchronous - 异步调用是否会创建一个单独的线程并释放 CPU 周期?