SupportMapFragment.getMapAsync-NullPointerException

时间:2018-09-19 02:06:22

标签: java android

使用com.google.android.gms:play-services:9.0.0

  

class扩展FragmentActivity

private void setMap()
            {

                map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2)).getMap();
                ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
                    public void onMapReady(GoogleMap googleMap) {
                        map = googleMap;
                    }
                });

                if (map !=null )
                {
                    zoom = 16;

                    map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
                    map.setMyLocationEnabled(true); 
                    UiSettings uis = map.getUiSettings();
                    uis.setZoomControlsEnabled(true);  
                    uis.setMyLocationButtonEnabled(true); 
                    uis.setScrollGesturesEnabled(true); 
                    uis.setZoomGesturesEnabled(true); 



                    String snippet = "";

                    for ( int i = 0 ; i < fe.length ;i++){

                        latitude = fe[i].getEastlongitude();
                        longitude = fe[i].getNorthernlongitude();

            if (latitude== 0)
                continue;

            addr = new LatLng (latitude, longitude);
            MarkerOptions mk = new MarkerOptions();
            mk.position(addr);
            mk.title(fe[i].getTitle());
            snippet = fe[i].getAddress();
            mk.snippet(snippet);
            mk.draggable(true);
            map.addMarker(mk);
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL); // Normal MapView

            if (index == i)
            {

                address = addr;
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(addr, zoom));
            }

        }


    }
}

我知道

  

java.lang.NullPointerException:尝试调用虚拟方法'void   com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'   在空对象引用上

enter image here

4 个答案:

答案 0 :(得分:0)

如果在onCreateView中调用getMap(),请尝试改用onViewCreated()。

尝试使用getChildFragmentManager()

mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

最后一个选项,您可以尝试MapView

<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);

答案 1 :(得分:0)

嘿,我认为您使用的是无效的资源ID 更改以下行

((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync()

像这样

((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id. fragment2)).getMapAsync()

希望您能理解问题。

答案 2 :(得分:0)

如果您在谈论 onCreateView 方法,那么您必须根据this使用片段:

  

在Google for Maps的最新更新中,片段仅支持MapView。

因此,在您的XML中而不是使用地图片段,请使用MapView:

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后在片段的 onCreateView 中:

mMapView = rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
try {
    MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
    e.printStackTrace();
}
mMapView.getMapAsync(this);

此外,根据MapView docs

  

此类的用户必须将所有生命周期方法从包含此视图的Activity或Fragment转发到此类中的相应方法。特别是,您必须转发以下方法:

     
      
  • onCreate(捆绑)
  •   
  • onStart()
  •   
  • onResume()
  •   
  • onPause()
  •   
  • onStop()
  •   
  • onDestroy()
  •   
  • onSaveInstanceState()
  •   
  • onLowMemory()
  •   

答案 3 :(得分:0)

修改了某些片段的设置并 解决了使用新的api键的问题。