访问地图时出现NullPointer异常

时间:2017-10-08 03:26:09

标签: android google-maps

我正在片段类中的地图上工作。

在布局文件中,我已按如下方式声明了地图:

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

然后在片段的onCreateView方法中,我包括以下内容:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_home, container, false);


        mapView = (MapView) v.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);


        mapView.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                map = mMap;
                map.setInfoWindowAdapter(new CustomInfoWindowOrigen(LayoutInflater.from(getActivity())));
                // For showing a move to my location button
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    return;
                }
                map.setMyLocationEnabled(true);


                LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
                Criteria criteria = new Criteria();

                Location location = locationManager.getLastKnownLocation(locationManager
                        .getBestProvider(criteria, false));


                if (location != null) {
                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                            new LatLng(location.getLatitude(), location
                                    .getLongitude()), 14));
                }
            }
        });

        return v;
    }

现在我需要在地图上添加标记或跟踪路线,但在对地图进行任何引用时会出现NullPointer异常。

喜欢这里:

 //LAS DOS DIRECCIONES
        if (!direccion_origen.equals("...En espera...")){

            estado_actual = "1";
            if (!direccion_destino.equals("...En espera...")){

                estado_actual = "3";
                btnorigen.setBackgroundColor(Color.GREEN);
                btndestino.setBackgroundColor(Color.GREEN);

                LatLng sydney = new LatLng(-33.852, 151.211);
        java.lang.NullPointerException ->       map.addMarker(new MarkerOptions().position(sydney)
                        .title("Marker in Sydney"));
                map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            }
        }

如何在代码中添加地图标记?

2 个答案:

答案 0 :(得分:1)

一个简单的解决方案是每次需要对地图的引用时调用getMapAsync。不会注意到微小的性能差异。

答案 1 :(得分:1)

你应该等到两个OnMapReadyCallback都完成了。

mapView.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap mMap) {
                    map = mMap;
                    map.setInfoWindowAdapter(new CustomInfoWindowOrigen(LayoutInflater.from(getActivity())));
                    // For showing a move to my location button
                    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                        return;
                    }
                    map.setMyLocationEnabled(true);


                LatLng sydney = new LatLng(-33.852, 151.211);
                map.addMarker(new MarkerOptions().position(sydney)
                        .title("Marker in Sydney"));
                map.moveCamera(CameraUpdateFactory.newLatLng(sydney));
                }