以编程方式创建MapView并添加标记导致片段中的空指针异常

时间:2017-01-20 21:27:44

标签: android google-maps fragment android-mapview

我正在尝试以编程方式创建MapView并添加MarkerPosition,如下所示:

MapView mapView = new MapView(getActivity());
((ViewGroup) rootView).addView(mapView);

GoogleMap googleMap = mapView.getMap();
googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(
                Double.parseDouble(
                        mEvent.getEventInfo().mEventData.mLat),
                Double.parseDouble(
                        mEvent.getEventInfo().mEventData.mLng)))
        .title("Marker"));

清单文件:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<permission
    android:name="com.example.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.permission.MAPS_RECEIVE" />

我收到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

4 个答案:

答案 0 :(得分:3)

因为你的googleMap没有加载但是意味着null并且你正在添加标记,这会导致崩溃。最好使用mapView.getMapAsync(context);而不是mapView.getMap();并实现此方法

    @Override
    public void onMapReady(final GoogleMap map) {
        if(map != null){
        // you are ready to add marker here        
        }
    }

答案 1 :(得分:2)

现在应该通过向您的视图添加MapFragment然后处理它来异步加载Google地图。这样的东西会起作用:

public class MainActivity extends Activity implements OnMapReadyCallback {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);


        MapView mapView = new MapView(this);
        ((LinearLayout) findViewById(R.id.mapWrapper)).addView(mapView);
        mapView.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap map) {

        map.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0))
                .title("Marker"));
    }

}

使用此布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapWrapper"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

答案 2 :(得分:1)

让您的活动实现OnMapReadyCallback并在OnMapReady()函数中添加标记。

参考:https://developers.google.com/maps/documentation/android-api/start

答案 3 :(得分:0)

事实证明,在谷歌播放服务版本11.8.0 onCreatea()onResume()是必看的MapView:

@Override public void setupMap(LatLng latLng) {
    if (mapView == null) {
      mapView = new MapView(getActivity());
      rootLayout.addView(mapView);
      mapView.onCreate(Bundle.EMPTY);
    }
    mapView.getMapAsync(map -> {
      map.clear();
      map.addCircle(MapUtil.makeDefaultGreenCircle(getActivity(), latLng));
    });
  }

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