【问题标题】:How to fix a nullPointerException in the map-object without destroying the onMapReady activity [duplicate]如何在不破坏 onMapReady 活动的情况下修复地图对象中的 nullPointerException [重复]
【发布时间】: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


【解决方案1】:

您可以考虑为您的SupportMapFragment 编写一个构造函数,并在进行片段事务之前先对其进行初始化。因此你不会得到NullPointerException

我的建议有点像以下。

// Declare this in your class as a public variable.
public SupportMapFragment mapFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mapFragment = new SupportMapFragment();

  // I suppose this is doing something that requires the context. 
  // Omit this for now
  // mapFragment.getMapAsync(this);

  // Do the fragment transaction here
}

然后在SupportMapFragmentonResume 方法中执行以下操作。

@Override
public void onResume() {
    super.onResume();
    getMapAsync(getActivity());
}

这只是一个示例代码。您可以考虑将参数传递给 SupportMapFragment 的构造函数,这将初始化必要的变量。我希望你能明白。

【讨论】:

  • 谢谢,我实现了你的想法,但应用程序在启动时仍然崩溃
  • 我已经编辑了我的答案。您现在可以检查一下,如果这能解决您的问题,请告诉我?
  • 我尝试了各种版本,但还是不行
猜你喜欢
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 2016-09-01
  • 2013-08-02
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多