【问题标题】:Google maps does not play for some devices and the app crashGoogle 地图无法在某些设备上播放,并且应用程序崩溃
【发布时间】:2016-03-17 18:31:04
【问题描述】:

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

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)”

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

提前谢谢你

【问题讨论】:

  • 崩溃仅在 Android Lollypop 或其他设备上发生
  • 这款平板电脑有安卓棒棒糖,三星 S6 也有安卓棒棒糖但运行良好,其他有 kitkat 或棉花糖的设备也运行良好@JagjitSingh
  • 如果您查看 getMap() 的文档,它会说“对于较新版本的 Google Play 服务,返回值可能始终为 null。”所以最好使用getMapAsync()
  • 好的,我该如何解决这个问题?
  • 尝试使用 getMapAsync 参考这里:-coderzpassion.com/android-google-maps-v2-tutorial-with-markers

标签: android google-maps


【解决方案1】:

试试这个解决方案:

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

【讨论】:

    【解决方案2】:

    您必须使用 onMapReady,对于棉花糖,您必须单独检查权限 -->

     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);
    
            }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多