模拟位置FATAL ERROR void android.location.LocationManager.removeTestProvider(java.lang.String)

时间:2016-07-19 21:31:07

标签: java android gps location

对于我的客户,我正在尝试测试他的餐厅查找器应用程序(哪个会找到靠近您所在位置的餐馆)。但由于这需要很多模拟位置,我使用地图实现了一个活动,我可以快速选择一个位置并模拟该区域的上下文。我遇到的问题是,在执行函数setMockLocation(locationMock)时,我得到一个致命的异常,不知道如何解决这个问题。

目前,我有这个类来处理Mock位置的设置。

package HIDDEN;

import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;

import com.google.android.gms.maps.model.LatLng;

public class locationHandler {

     public static LocationManager mLocationManager;

    public static void setMockLocation(LatLng location) {
        mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER);
        mLocationManager.addTestProvider
                (
                        LocationManager.GPS_PROVIDER,
                        "requiresNetwork" == "",
                        "requiresSatellite" == "",
                        "requiresCell" == "",
                        "hasMonetaryCost" == "",
                        "supportsAltitude" == "",
                        "supportsSpeed" == "",
                        "supportsBearing" == "",

                        android.location.Criteria.POWER_LOW,
                        android.location.Criteria.ACCURACY_FINE
                );

        Location newLocation = new Location(LocationManager.GPS_PROVIDER);



        newLocation.setLatitude (location.latitude);
        newLocation.setLongitude(location.longitude);

        newLocation.setAccuracy(500);

        mLocationManager.setTestProviderEnabled
                (
                        LocationManager.GPS_PROVIDER,
                        true
                );

        mLocationManager.setTestProviderStatus
                (
                        LocationManager.GPS_PROVIDER,
                        LocationProvider.AVAILABLE,
                        null,
                        System.currentTimeMillis()
                );

        mLocationManager.setTestProviderLocation
                (
                        LocationManager.GPS_PROVIDER,
                        newLocation
                );
    }
}

我也在调试AndroidManifest.xml中设置了它,因为它不允许我在主清单中设置它。

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

要调用setMockLocation函数,我使用以下代码,我将通过LatLang传递我想要模拟的所需位置。

locationHandler.setMockLocation(locationMock);

在我的手机上执行此操作以进行调试并单击按钮以模拟该位置时,我收到以下错误。

/AndroidRuntime: FATAL EXCEPTION: main
    Process: HIDDEN, PID: 10688
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.removeTestProvider(java.lang.String)' on a null object reference
        at HIDDEN.locationHandler.setMockLocation(locationHandler.java:17)
        at HIDDEN.MapsActivity$5.onClick(MapsActivity.java:220)
        at android.view.View.performClick(View.java:5697)
        at android.view.View$PerformClick.run(View.java:22526)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7224)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:134)

感谢所有花时间为我看这个的人。

0 个答案:

没有答案