本文介绍了在测试期间以编程方式设置genymotion gps会导致“SecurityException:invalid package name”设置纬度时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试时,我通过Genymotion设置lat long,如下所示:

When testing, I am setting the lat long via Genymotion like so:

 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)


推荐答案

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

In a test Instrumentation.getContext() returns the context of the tests. To access the context of the app being tested, you need to call Instrumentation.getTargetContext() instead. You should also consider using InstrumentationRegistry. This is now the preferred access point for Instrumentation instances.

这篇关于在测试期间以编程方式设置genymotion gps会导致“SecurityException:invalid package name”设置纬度时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:55