Google地图在位置更新时返回null

时间:2018-05-16 20:18:43

标签: android google-maps

几周以来,当程序试图在地图上放置标记时,我得到空指针异常。我不知道为什么,我尝试了几种解决方案,但它们没有用。我在几台设备上收到此错误。例如,100个设备没有错误,但在5个或更多设备中出现此错误 我也无法使用.getMap()函数。只有getMapAsync可用。

以下是我从Firebase获得的错误:

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.animateCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference

以下是Google地图片段:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map1"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.ahmetbesli.eczanem.MapsActivityPicker"
        />

以下是我的程序代码:

public class MapsActivityPicker extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
        SharedPreferences sharedPreferences;
        GoogleMap mGoogleMap;
        SupportMapFragment mapFrag;
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
        Location mLastLocation;
        Marker mCurrLocationMarker;
        FusedLocationProviderClient mFusedLocationClient;
        LocationManager mLocationManager;

        ArrayList<MarkerData> markers = new ArrayList<>();
        int counter = 1;
        int resumeCounter = 2;
        boolean add = true;
        LatLng midLatLng;
        LatLng latLngMaps;

        ImageView direction;
        ImageView bullet;
        ImageView share;
        ImageView nearMe;
        ImageView call;
        CountDownTimer timer;
        String pharmacyName;
        String pharmacyAddress;
        String markerPhone = null;
        Double markerLatitude;
        Double markerLongitude;

        private InterstitialAd mInterstitialAd;
        String shortURL;
        LocationCallback mLocationCallback;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps_picker);




            mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

            mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
            buildGoogleApiClient();
            mapFrag.getMapAsync(this);
            mLocationManager = (LocationManager) MapsActivityPicker.this
                    .getSystemService(Context.LOCATION_SERVICE);

            direction = findViewById(R.id.directionPicker);
            bullet = findViewById(R.id.bulletPicker);
            share = findViewById(R.id.sharePicker);
            nearMe = findViewById(R.id.accessPicker);
            call = findViewById(R.id.callPicker);

            direction.setOnClickListener(this);
            bullet.setOnClickListener(this);
            share.setOnClickListener(this);
            nearMe.setOnClickListener(this);
            call.setOnClickListener(this);

            sharedPreferences = this.getApplication().getSharedPreferences(Config.app_name,MODE_PRIVATE);

            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId("xxxxxxxxxx");
            mInterstitialAd.loadAd(new AdRequest.Builder().build());


            timer = new CountDownTimer(5000, 1000)
            {
                public void onTick(long millisUntilFinished) {
                }

                public void onFinish() {
                    if (add) {
                        if (mInterstitialAd != null) {
                            if (mInterstitialAd.isLoaded()) {
                                mInterstitialAd.show();
                            }
                        }
                    }
                }
            };
            timer.start();
        }

        @Override
        public void onPause() {
            super.onPause();

            if (mFusedLocationClient != null) {
                mFusedLocationClient.removeLocationUpdates(mLocationCallback);
            }
        }

        @Override
        public void onMapReady(final GoogleMap googleMap) {

            mGoogleMap = googleMap;
            mGoogleMap.getUiSettings().setMapToolbarEnabled(false);
            mGoogleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MapsActivityPicker.this));
            mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                public void onMapClick(LatLng latLng) {
                    bullet.setVisibility(View.VISIBLE);
                    share.setVisibility(View.GONE);
                    direction.setVisibility(View.GONE);
                    nearMe.setVisibility(View.VISIBLE);
                    call.setVisibility(View.GONE);
                }
            });
            mGoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    Intent intent = new Intent(MapsActivityPicker.this, HomePageShared.class);
                    intent.putExtra("name",marker.getTitle());
                    intent.putExtra("address",marker.getSnippet());
                    intent.putExtra("phone",markerPhone);
                    intent.putExtra("latitude",marker.getPosition().latitude);
                    intent.putExtra("longitude",marker.getPosition().longitude);
                    intent.putExtra("userLatitude", midLatLng.latitude);
                    intent.putExtra("userLongitude",midLatLng.longitude);
                    startActivity(intent);
                }
            });
            mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                Marker currentShown;
                @Override
                public boolean onMarkerClick(Marker marker) {

                    if (marker.equals(currentShown)) {
                        marker.hideInfoWindow();
                        currentShown = null;
                    } else {
                        marker.showInfoWindow();
                        currentShown = marker;
                    }

                    pharmacyName = marker.getTitle();
                    pharmacyAddress = marker.getSnippet();
                    markerLatitude = marker.getPosition().latitude;
                    markerLongitude = marker.getPosition().longitude;

                    for(int i = 0; i < markers.size(); i++){
                        if(markers.get(i).getName().equals(pharmacyName)){
                            markerPhone = markers.get(i).getPhone();
                        }
                    }


                    shortURL = "http://maps.google.com/maps?daddr=" + markerLatitude + "," + markerLongitude;


                    latLngMaps = marker.getPosition();
                    share.setVisibility(View.VISIBLE);
                    direction.setVisibility(View.VISIBLE);
                    bullet.setVisibility(View.GONE);
                    nearMe.setVisibility(View.GONE);
                    call.setVisibility(View.VISIBLE);

                    return true;
                }

            });

            googleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
                @Override
                public void onCameraIdle() {
                    //get latlng at the center by calling
                    midLatLng = googleMap.getCameraPosition().target;
                    //request();
                    requestRetrofit();
                    googleMap.clear();

                }
            });
            if(sharedPreferences.getBoolean(Config.theme,false)){
                mGoogleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.night_mode));
            }
        }

        protected void getGPS() {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    this);
            alertDialogBuilder
                    .setMessage("Konum Servisiniz kapalı. Eczaneleri görebilmek için lütfen açınız.")
                    .setCancelable(false)
                    .setPositiveButton("Konum Servisini Aç",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int id) {
                                    Intent callGPSSettingIntent = new Intent(
                                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    MapsActivityPicker.this.startActivity(callGPSSettingIntent);
                                }
                            });
            alertDialogBuilder.setNegativeButton("Kapalı Kalsın",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = alertDialogBuilder.create();
            alert.show();
        }

        public void requestRetrofit(){
            ApiInterface apiInterface = ApiClient.getRetrofit().create(ApiInterface.class);
            Call<ArrayList<Eczane>> call = apiInterface.getPharmacies(new com.ahmetbesli.eczanem.Request(String.valueOf(midLatLng.latitude),String.valueOf(midLatLng.longitude)));
            call.enqueue(new Callback<ArrayList<Eczane>>() {
                @Override
                public void onResponse(Call<ArrayList<Eczane>> call, retrofit2.Response<ArrayList<Eczane>> response) {
                    markers.clear();
                    if (response.body().get(0) != null) {
                        for (int i = 0; i < 5; i++) {
                            if (!response.body().get(i).getLatitude().equals("undefined") && !response.body().get(i).getLongitude().equals("undefined") && !response.body().get(i).getLatitude().equals("") && !response.body().get(i).getLongitude().equals("")) {
                                createMarker(Double.valueOf(response.body().get(i).getLatitude()),
                                        Double.valueOf(response.body().get(i).getLongitude()), response.body().get(i).getName(),
                                        response.body().get(i).getAdress(), response.body().get(i).getPhone(), R.mipmap.marker);
                            }
                        }
                    }
                }

                @Override
                public void onFailure(Call<ArrayList<Eczane>> call, Throwable t) {

                }
            });
        }

        protected void starter() {
            //Initialize Google Play Services
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {
                    //Location Permission already granted
                    buildGoogleApiClient();
                } else {
                    //Request Location Permission
                    checkLocationPermission();
                }
            } else {
                buildGoogleApiClient();
            }
        }

        protected synchronized void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
            mGoogleApiClient.connect();
        }

        @Override
        public void onConnected(Bundle bundle) {
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setInterval(120000); // two minute interval
            mLocationRequest.setFastestInterval(120000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
            }
        }

        @Override
        public void onConnectionSuspended(int i) {
        }

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        }

        public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

        private void checkLocationPermission() {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {

                // Should we show an explanation?
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)) {

                    // Show an explanation to the user *asynchronously* -- don't block
                    // this thread waiting for the user's response! After the user
                    // sees the explanation, try again to request the permission.
                    new AlertDialog.Builder(this)
                            .setTitle("Konum Servisi için izin gerekli.")
                            .setMessage("Bu uygulama konum servisi iznine ihtiyaç duymaktadır. Lütfen kabul ediniz.")
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    //Prompt the user once explanation has been shown
                                    ActivityCompat.requestPermissions(MapsActivityPicker.this,
                                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                            MY_PERMISSIONS_REQUEST_LOCATION);
                                }
                            })
                            .create()
                            .show();


                } else {
                    // No explanation needed, we can request the permission.
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            MY_PERMISSIONS_REQUEST_LOCATION);
                }
            }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_LOCATION: {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                        // permission was granted, yay! Do the
                        // location-related task you need to do.
                        if (ContextCompat.checkSelfPermission(this,
                                Manifest.permission.ACCESS_FINE_LOCATION)
                                == PackageManager.PERMISSION_GRANTED) {

                            if (mGoogleApiClient == null) {
                                buildGoogleApiClient();
                            }
                        }

                    } else {
                        // permission denied, boo! Disable the
                        // functionality that depends on this permission.
                        Toast.makeText(this, "İzin Reddedildi", Toast.LENGTH_LONG).show();
                    }
                }

                // other 'case' lines to check for other
                // permissions this app might request
            }
        }

        @Override
        public void onResume() {
            super.onResume();
            if (resumeCounter <= 2) {
                starter();
                resumeCounter++;
            }
            if (counter == 0) {
                if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    starter();
                    counter++;
                } else {
                    getGPS();
                    counter++;
                }
            }
        }

        protected void createMarker(double latitude, double longitude, String title, String snippet, String phone, int iconResID) {
            MarkerData marker = new MarkerData(title, snippet, phone, latitude, longitude);
            markers.add(marker);

            mGoogleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(latitude, longitude))
                    .anchor(0.5f, 0.5f)
                    .title(title)
                    .snippet(snippet)
                    .icon(BitmapDescriptorFactory.fromResource(iconResID)));
        }

        @Override
        protected void onStart(){
            super.onStart();
            mLocationCallback = new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    for (Location location : locationResult.getLocations()) {
                        mLastLocation = location;
                        if (mCurrLocationMarker != null) {
                            mCurrLocationMarker.remove();
                        }

                        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10.7f));
                    }
                }
            };
        }

    }

1 个答案:

答案 0 :(得分:1)

onStart方法中,您可能在未分配mGoogleMap.animateCamera()时调用onMapReady。它是在if(mGoogleMap != null){ mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10.7f)); } 中分配的 你需要检查它是否为空。

onMapReady()

我怀疑你有竞争条件:在某些设备上onLocationResult()方法在onLocationResult()之前被调用...所以一切正常。但在onMapReady()方法之前调用其他设备null,因此mGoogleMap为//create a class variable mLatLng private LatLng mLatLng; !因此,您必须检查是否&#34; mGoogleMap&#34;已被分配。

请改为:

onLocationResult()

现在将public void onLocationResult(LocationResult locationResult) { for (Location location : locationResult.getLocations()) { mLastLocation = location; if (mCurrLocationMarker != null) { mCurrLocationMarker.remove(); } mLatLng = new LatLng(location.getLatitude(), location.getLongitude()); moveMapTo(); } 方法中的代码更改为:

private void moveMapTo(){
    if(mLatLng == null) return;

    if(mGoogleMap != null){
        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.7f));
    }
}

添加方法:

onMapReady()

moveMapTo(); 方法中,您只需要添加:

onLocationResult()

这种方式在onMapReady()之前调用onMapReady()变量&#34; mLatLng&#34}已定义且有效,但&#34; mGoogleMap&#34;为null - 这会导致错误。一旦&#34; mGoogleMap&#34;已在moveMapTo() onMapReady()中正确定义并再次调用,并将您的动画设置为正确的位置。但是,如果在onLocationResult()之前调用了onLocationResult(),那么&#34; mGoogleMap&#34;已经分配,​​并且在Clip中一切都按预期工作。