android.location.Location setters not working as e

2019-05-18 10:02发布

问题:

I need to create a Location object from some latitude and longitude values, without going through a location provider. I do the following:

Location l = new Location("");
l.setLatitude(32);
l.setLongitude(43);

The values, however, are not set properly, since both getLatitude() and getLongitude() return 0.

Can you please tell me where is the problem? Isn't this the correct way to create a "custom" Location?

回答1:

I figured it out eventually. When unit testing using the Android APIs, you need to use the instrumentation framework. I was using JUnit alone, thus the malfunctioning. I resolved by using Roboelectric, a tool that sort of emulates the Android platform allowing you to run unit tests that use Android APIs without bringing up a device emulator every time.