简体   繁体   中英

Android - error when try to test GPS on Emulator

i wanna test a simple brief of using GPS on android. so i write a simple code like this :

public class main extends Activity{
LocationManager mLocationManager;
TextView tv;
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView)findViewById(R.id.myLocationText);
    mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String locationprovider = mLocationManager.getBestProvider(criteria, true);
    Location mLocation = mLocationManager.getLastKnownLocation(locationprovider);

    tv.setText("Last location lat:" + mLocation.getLatitude() + "long:" + mLocation.getLongitude());
}

}

also, on AndroidManifest i added uses-permission like this :

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

but when i try to run at android emulator i get Error "Stopped Unexpectedly" and this apps closed. i added some try with DDMS too. but i dont have any idea about this error.

I've also had similar problems in real devices. The frequency at which the listener was receiving new locations was too high to update my TextViev. I solved it with a bit of synchronization, and skipping View refresh if it was being refreshed at that moment.

In your case, as you are not using a listener, I'd check for NullPointerExceptions .

You are probably not using Google API for the Android Emulator that's why you are cant access the Google Map APı and get a crash like that.

Check this for more info about setting emulator for usage with Maps.

http://code.google.com/android/add-ons/google-apis/maps-overview.html

It is because you are using it from the emulator and it doesn't have any gps related hardware.

here is a solution

To send mock location data from the command line:

Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory. Connect to the emulator console:

telnet localhost <console-port>

Send the location data: geo fix to send a fixed geo-location. This command accepts a longitude and latitude in decimal degrees, and an optional altitude in meters. For example:

geo fix -121.45356 46.51119 4392

geo nmea to send an NMEA 0183 sentence. This command accepts a single NMEA sentence of type '$GPGGA' (fix data) or '$GPRMC' (transit data). For example:

geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62

source : http://developer.android.com/guide/topics/location/obtaining-user-location.html

also see this link: http://developer.android.com/guide/developing/devices/emulator.html#console

It is an API KEY issues. You can try to request a new API KEY on google map API key.
Your code is correct and didn't show any errors.

Perhaps this answer can help you solve your problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM