【发布时间】:2019-10-08 02:21:56
【问题描述】:
我的应用应该在第一个片段中显示地图。该应用程序由导航抽屉组织。地图显示没有问题,但没有显示我提供的默认缩放和位置。
我在创建应用程序时遇到的第一个错误是NullPointerException。我知道如何解决它,所以我说我的地图片段(mapFragment)不为空。该应用程序现在开始工作,但我在方法onMapReady 中编写的代码以及用于获取当前位置并将相机移动到该特定位置的按钮没有显示/没有工作。
我也尝试更改地图的代码,但我总是在同一个地方结束。如果我不写map!=null,我会得到NullPointerException。如果我写它,应用程序不会正确放大到特定位置。
在Mainactivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.mapFragment);
if(mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
这是我必须阻止NullPointerException(mapFragment)
在这里我设置了默认位置和缩放,它不会应用于地图(虽然有mapFragment!= null):
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)
{
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
LatLng Luxembourg = new LatLng(49.611622, 6.131935);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(Luxembourg, 14));
}
这是检索用户当前位置的方法(在 mapFragment!= null 时也不起作用):
@Override
public void onLocationChanged(Location location) {
lastLocation = location;
if(currentUserLocationMarker != null)
{
currentUserLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("current user location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker
(BitmapDescriptorFactory.HUE_RED));
currentUserLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomBy(14));
if(googleApiClient != null)
{
LocationServices.FusedLocationApi.removeLocationUpdates
(googleApiClient, this);
}
}
这是 mapFragment 本身:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
</FrameLayout>
我希望地图具有我提供的默认位置和缩放以及将地图移动到用户当前位置的按钮。
【问题讨论】:
-
如果 mapFragment 为空,则所有赌注都关闭;考虑片段是主要活动布局的子项的可能性:使用
getChildFragmentManager- 请参阅stackoverflow.com/a/26598640/2711811。 -
@Fabtômas 非常感谢您将此问题标记为“什么是 NullPointerException,我该如何解决?”我的问题是一个完全不同的问题,如果您完全通读它,您就会知道我的实际问题是什么。我已经知道如何修复 NullPointerException,事实上,在我的代码中我已经修复了它!我的问题是修复后会发生什么,请阅读整个问题,不要快速判断!
标签: java android nullpointerexception