Android 6.0应用在Google地图null

时间:2016-07-18 10:26:20

标签: android google-maps

我的应用程序使用带有地图片段和列表片段的swip选项卡运行。 该应用程序在所有设备上工作正常,API从4.3到5.1但是当我在带有API 6.0的Android设备上运行时,我发现了这个错误:

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

这是一个文件片段代码生成错误:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    v = inflater.inflate(R.layout.fragment_mappa, null, false);

    // To get City-Name from coordinates
    GPSTracker gpsTracker = new GPSTracker(getActivity());

    String cityName = null;
    Geocoder gcd = new Geocoder(getContext(), Locale.getDefault());
    List<Address> addresses = null;
    try {
        addresses = gcd.getFromLocation(gpsTracker.getLatitude(), gpsTracker.getLongitude(), 1);
        if (addresses.size() > 0) {
            System.out.println(addresses.get(0).getLocality());
            cityName = addresses.get(0).getLocality();
        }
        else {
            //gpsTracker.showSettingsAlert();
        }
        if (cityName == "Rome"){
            nome_citta = "Roma";
        }
        if (cityName == "Florence") {
            nome_citta = "Firenze";
        }
        if (cityName == "Valencia") {
            nome_citta = "Valence";
        }
        nome_citta = cityName;

    } catch (IOException e) {
        e.printStackTrace();

    }

    /*ImageButton b = (ImageButton) v.findViewById(R.id.refresh);
    b.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new TabFragment()).commit();
        }
    });*/






    GPSTracker mGPS = new GPSTracker(getActivity());
    GPSService mGPSService = new GPSService(getActivity());
    Double la = mGPSService.getLatitude();
    /*mGPSService.getLocation();
    if (mGPS.canGetLocation()) {
        address = mGPSService.getLocationAddress();
        Log.d("Address","Adrress"+address);
    } else {
        mGPS.showSettingsAlert();
    }*/



    processMap(v,10);

    // map.setOnCameraChangeListener(getCameraChangeListener());

    mClusterManager = new ClusterManager<Car2GoClusterItem>(getActivity(), map);

    map.setOnCameraChangeListener(mClusterManager);
    map.setInfoWindowAdapter(mClusterManager.getMarkerManager());
    map.setOnMarkerClickListener(mClusterManager);

    mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<Car2GoClusterItem>() {
        @Override
        public boolean onClusterItemClick(Car2GoClusterItem item) {
            clickedClusterItem = item;
            return false;
        }
    });

    //      map.setOnMarkerClickListener(mClusterManager);
    return v;



}

public GoogleMap.OnCameraChangeListener getCameraChangeListener()
{
    return new GoogleMap.OnCameraChangeListener()
    {
        @Override
        public void onCameraChange(CameraPosition position)
        {
            Log.d("Zoom", "Zoom: " + position.zoom);

            if(previousZoomLevel<=15 && position.zoom>15)
            {
                map.clear();
                processMap(v, 16);
            }

            if(previousZoomLevel>=15 && position.zoom<15)
            {
                map.clear();
                processMap(v, 14);
            }

            previousZoomLevel = position.zoom;
        }
    };
}

public void processMap(View v, int zoom) {
    if(map == null) {
        map = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMap();

    }


    if(map != null){
     // othe code here ....................
    }

为什么我只在Android 6上出现此错误? 我该如何解决这个问题? 感谢

如果我设置

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    v = inflater.inflate(R.layout.fragment_mappa, null, false);

    // To get City-Name from coordinates
    GPSTracker gpsTracker = new GPSTracker(getActivity());

    String cityName = null;
    Geocoder gcd = new Geocoder(getContext(), Locale.getDefault());
    List<Address> addresses = null;
    try {
        addresses = gcd.getFromLocation(gpsTracker.getLatitude(), gpsTracker.getLongitude(), 1);
        if (addresses.size() > 0) {
            System.out.println(addresses.get(0).getLocality());
            cityName = addresses.get(0).getLocality();
        }
        else {
            //gpsTracker.showSettingsAlert();
        }
        if (cityName == "Rome"){
            nome_citta = "Roma";
        }
        if (cityName == "Florence") {
            nome_citta = "Firenze";
        }
        if (cityName == "Valencia") {
            nome_citta = "Valence";
        }
        nome_citta = cityName;

    } catch (IOException e) {
        e.printStackTrace();

    }

    /*ImageButton b = (ImageButton) v.findViewById(R.id.refresh);
    b.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new TabFragment()).commit();
        }
    });*/






    GPSTracker mGPS = new GPSTracker(getActivity());
    GPSService mGPSService = new GPSService(getActivity());
    Double la = mGPSService.getLatitude();
    /*mGPSService.getLocation();
    if (mGPS.canGetLocation()) {
        address = mGPSService.getLocationAddress();
        Log.d("Address","Adrress"+address);
    } else {
        mGPS.showSettingsAlert();
    }*/



    processMap(v,10);

    // map.setOnCameraChangeListener(getCameraChangeListener());

    mClusterManager = new ClusterManager<Car2GoClusterItem>(getActivity(), map);

    map.setOnCameraChangeListener(mClusterManager);
    map.setInfoWindowAdapter(mClusterManager.getMarkerManager());
    map.setOnMarkerClickListener(mClusterManager);

    mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<Car2GoClusterItem>() {
        @Override
        public boolean onClusterItemClick(Car2GoClusterItem item) {
            clickedClusterItem = item;
            return false;
        }
    });

    //      map.setOnMarkerClickListener(mClusterManager);
    return v;



}

public GoogleMap.OnCameraChangeListener getCameraChangeListener()
{
    return new GoogleMap.OnCameraChangeListener()
    {
        @Override
        public void onCameraChange(CameraPosition position)
        {
            Log.d("Zoom", "Zoom: " + position.zoom);

            if(previousZoomLevel<=15 && position.zoom>15)
            {
                map.clear();
                processMap(v, 16);
            }

            if(previousZoomLevel>=15 && position.zoom<15)
            {
                map.clear();
                processMap(v, 14);
            }

            previousZoomLevel = position.zoom;
        }
    };
}

public void processMap(View v, int zoom) {


    if(map == null) {
        map = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);




    }


    if(map != null){
    }

并实现OnMapReadyCallback我在这一行有错误:

map = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);

错误getMapAsync返回GoogleMap

1 个答案:

答案 0 :(得分:3)

Android 6.0中不推荐使用

getMap(),请使用getMapAsync()

  

以前不推荐使用的getMap()函数不再可用   Google Play服务SDK。 (它仍然在谷歌上市   播放服务APK,提供给Android设备。)getMap()   自2014年12月起,该函数已被弃用。请参阅发布博客   发布有关从getMap()转换为getMapAsync()的帮助。

更多信息:

https://developers.google.com/maps/documentation/android-api/releases

改变这个:

public void processMap(View v, int zoom) {
    if(map == null) {
        map = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMap();

    }


    if(map != null){
     // othe code here ....................
    }

为此:

public void processMap(View v, int zoom) {
    if(map == null) {
        map = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);

    }


    if(map != null){
     // othe code here ....................
    }

@Override
 public void onMapReady(GoogleMap map) {
     mMap = map;
     doStuff();
 }

并实施OnMapReadyCallback

更多信息:

http://googlegeodevelopers.blogspot.co.uk/2016/06/marker-zindex-and-more-come-to-google.html