OnLocationChanged持续触发多次,并导致堆栈溢出。

这是进口:

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.*; //addon-google apis 19 google-play-services.jar to libs folder and import it via build path -> configure build path ->Add External Jars


这是LocationListener:

 private LocationListener mLocationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {

            Log.e(TAG, "onLocationChanged>>>>>>>>>>>>>>>>>>");

            LatLong latLng = new LatLong(location.getLatitude(), location.getLongitude());

            centerPoint = latLng; //assign to global current location
            Log.e(TAG, "onLocationChanged>>>>>>>>>>>>>>>>>>centerPoint" + centerPoint);

            currentSpeed = location.getSpeed();
            Log.e(TAG, "onLocationChanged speed>>>>>>>>>>>>>>>>>>"+ currentSpeed);

            Log.e(TAG, "onLocationChanged: PROVIDER:>>>>>>>>>>>>>>>>"+location.getProvider().toString());

            // GPS location
            if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
                Log.e(TAG, "onLocationChanged: GPS_PROVIDER>>>>>>>>>>>>>>>>");
          }

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

            String statusString = "Unknown";

            switch (status) {

                case LocationProvider.OUT_OF_SERVICE:
                    statusString = "Out of service";
                    Log.e(TAG, "Out of service>>>>>>>>>>>>>>>>");
                    break;

                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    statusString = "Temporary unavailable";
                    Log.e(TAG, "Temporary unavailable>>>>>>>>>>>>>>>>");
                    break;

                case LocationProvider.AVAILABLE:
                    statusString = "Available";
                    Log.e(TAG, "AVAILABLE>>>>>>>>>>>>>>>>");
                    break;
            }

            Toast.makeText(
                    MainActivity.this,
                    String.format("Provider '%s' status: %s", provider, statusString),
                    Toast.LENGTH_SHORT)
            .show();
        }

        @Override
        public void onProviderEnabled(String provider) {

            Toast.makeText(
                    MainActivity.this, String.format("Provider '%s' enabled", provider), Toast.LENGTH_SHORT).show();

            // Remove strike-thru in label
            if (provider.equals(LocationManager.GPS_PROVIDER)) {
                Log.e(TAG, "GPS_PROVIDER>>>>>>>>>>>>>>>>");
            }

            if (provider.equals(LocationManager.NETWORK_PROVIDER)) {
                Log.e(TAG, "NETWORK_PROVIDER>>>>>>>>>>>>>>>>");
            }
        }

        @Override
        public void onProviderDisabled(String provider) {

            Toast.makeText(
                    MainActivity.this, String.format("Provider '%s' disabled", provider), Toast.LENGTH_SHORT).show();

            // Set strike-thru in label and hide accuracy circle
            if (provider.equals(LocationManager.GPS_PROVIDER)) {
                Log.e(TAG, "onProviderDisabled: GPS_PROVIDER>>>>>>>>>>>>>>>>");

            }

            if (provider.equals(LocationManager.NETWORK_PROVIDER)) {
                Log.e(TAG, "onProviderDisabled: NETWORK_PROVIDER>>>>>>>>>>>>>>>>");

            }
        }
    };

    /**
     * Location Source for google maps 'my location' layer.
     */
    private LocationSource mLocationSource = new LocationSource() {

        @Override
        public void activate(OnLocationChangedListener onLocationChangedListener) {

            mOnLocationChangedListener = onLocationChangedListener;
        }

        @Override
        public void deactivate() {

            mOnLocationChangedListener = null;
        }
    };


这是logcat错误日志:

02-15 12:30:37.240: I/dalvikvm(9993): threadid=1: stack overflow on call to Ljava/lang/System;.arraycopy:VLILII
02-15 12:30:37.240: I/dalvikvm(9993):   method requires 20+20+0=40 bytes, fp is 0xb028a324 (36 left)
02-15 12:30:37.240: I/dalvikvm(9993):   expanding stack end (0xb028a300 to 0xb028a000)
02-15 12:30:37.240: I/dalvikvm(9993): Shrank stack (to 0xb028a300, curFrame is 0xb028fec4)
02-15 12:30:37.280: D/AndroidRuntime(9993): Shutting down VM
02-15 12:30:37.280: W/dalvikvm(9993): threadid=1: thread exiting with uncaught exception (group=0xb1a76b90)
02-15 12:30:37.520: E/AndroidRuntime(9993): FATAL EXCEPTION: main
02-15 12:30:37.520: E/AndroidRuntime(9993): Process: com.example.offlinemapsforgesample, PID: 9993
02-15 12:30:37.520: E/AndroidRuntime(9993): java.lang.StackOverflowError
02-15 12:30:37.520: E/AndroidRuntime(9993):     at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:132)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at java.lang.RealToString.freeFormat(RealToString.java:232)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at java.lang.RealToString.convertDouble(RealToString.java:120)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at java.lang.RealToString.appendDouble(RealToString.java:63)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at java.lang.StringBuilder.append(StringBuilder.java:184)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at org.mapsforge.core.model.LatLong.toString(LatLong.java:99)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at java.lang.StringBuilder.append(StringBuilder.java:202)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at com.example.offlinemapsforgesample.MainActivity$2.onLocationChanged(MainActivity.java:1874)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at com.example.offlinemapsforgesample.MainActivity$2.onLocationChanged(MainActivity.java:1917)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at com.example.offlinemapsforgesample.MainActivity$2.onLocationChanged(MainActivity.java:1917)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at com.example.offlinemapsforgesample.MainActivity$2.onLocationChanged(MainActivity.java:1917)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at com.example.offlinemapsforgesample.MainActivity$2.onLocationChanged(MainActivity.java:1917)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at com.example.offlinemapsforgesample.MainActivity$2.onLocationChanged(MainActivity.java:1917)
02-15 12:30:37.520: E/AndroidRuntime(9993):     at com.example.offlinemapsforgesample.MainActivity$2.onLocationChanged(MainActivity.java:1917)

最佳答案

根据doc,当新位置可用时,将调用onLocationChanged,因此当传感器(GPS,网络...)获得新位置时,这种情况会经常发生。为了防止出现这种情况,您可以将最新的位置存储在变量中,并借助Timer在最后存储的位置上执行业务逻辑。
否则,您可以使用GoogleApiClient执行此操作:

LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(1000);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);

LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());


根据您的堆栈跟踪,堆栈溢出不是来自onLocationChanged,而是来自您编写在其中的业务逻辑。

关于android - onLocationChanged触发多个时间并导致堆栈溢出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35415876/

10-12 03:14