1

测试时,我通过 Genymotion 设置纬度,如下所示:

 package com.mypackage.name;

 public class TestGps extends ActivityInstrumentationTestCase2<MyClass>{

    ... //setup just calls super.setup();

    public void testLocationButton(){
       Gps gps = GenymotionManager.getGenymotionManager(getInstrumentation().getContext()).getGps();
       gps.setStatus(Gps.Status.ENABLED);
       gps.setLatitude(40.7127); // the error happens here
       gps.setLongitude(74.0059);
   }
}

运行时出现以下问题:

java.lang.SecurityException: invalid package name: com.mypackage.name
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1499)
at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:582)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:867)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:490)
at com.genymotion.api.Gps.waitForTargetLocation(Gps.java:271)
at com.genymotion.api.Gps.setLatitude(Gps.java:134)
4

1 回答 1

4

在测试中返回测试Instrumentation.getContext()的上下文。要访问正在测试的应用程序的上下文,您需要改为调用。您还应该考虑使用. 现在这是实例的首选访问点。Instrumentation.getTargetContext()InstrumentationRegistryInstrumentation

于 2016-02-03T23:56:42.460 回答