谷歌地图无法播放某些设备而应用程序崩溃

时间:2016-03-17 09:53:11

标签: android google-maps

我使用此代码来实例化Google Map:

map = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.map));
    mMap = map.getMap();
    mMap.setMyLocationEnabled(true);

这种用途适用于许多设备,如nexus 5和三星Galaxy S6,但我有一个平板电脑(SONY XPERIA Z3),当我尝试导航到这个片段时,应用程序崩溃与logcat:

  

致命的例外:主要                                                                                  处理:com.example.veriah.loneworker,PID:20646                                                                                  java.lang.NullPointerException:尝试调用虚方法' void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)'在空对象引用上

此外,我已经激活了此平板电脑的位置。 有人知道为什么会这样吗?

提前谢谢

2 个答案:

答案 0 :(得分:2)

试试这个解决方案:

map.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        mMap = googleMap;
                        googleMap.setMyLocationEnabled(true);
                    }
                });

答案 1 :(得分:0)

你必须使用onMapReady,对于marshmallow,你必须单独检查权限 - >

 public LocationManager locationmaneger;

 locationmaneger = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                lon = location.getLongitude();
                lat = location.getLatitude();
                System.out.println(lon + " " + lat);
                // LatLng mycurrentposition = new LatLng(lat, lon);
                //mMap.setMyLocationEnabled(true);
                mMap.clear();
                int i;

                mMap.addMarker(new MarkerOptions()
                        .title("My Location")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.myloc))
                        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                        .position(new LatLng(lat, lon)))
                        .setDraggable(true);



                //camera
               LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
               /* CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
                mMap.animateCamera(cameraUpdate);*/

                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(latLng)      // Sets the center of the map to Mountain View
                        .zoom(17)                   // Sets the zoom
                        .bearing(rotate)                // Sets the orientation of the camera to east
                        .tilt(80)                   // Sets the tilt of the camera to 30 degrees
                        .build();                   // Creates a CameraPosition from the builder
                mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                if(rotate == 150 )
                {
                    rotate = 200;
                }
                else{
                    rotate = 150;
                }


                locationmaneger.removeUpdates(this);


            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);

            }
        };

 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode){
        case 10:
            if(grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)

            locationmaneger.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
            locationmaneger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

            return;
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    System.out.println("i am on mapready");

    if(isNetworkConnected())
    {
        mMap = googleMap;
           if (Build.VERSION.SDK_INT >= 23) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.INTERNET},10);

                return;
            }
        }else{
            System.out.println(lon + " " + lat);
        locationmaneger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 120000 , locationListener);
            locationmaneger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 120000 , locationListener);

        }

}