简体   繁体   English

Android - 尝试在模拟器上测试GPS时出错

[英]Android - error when try to test GPS on Emulator

i wanna test a simple brief of using GPS on android. 我想测试一下在android上使用GPS的简单简介。 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 : 另外,在AndroidManifest上我添加了use-permission,如下所示:

    <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. 但是当我尝试在Android模拟器上运行时,我得到错误“意外停止”并关闭此应用程序。 i added some try with DDMS too. 我也加入了DDMS的尝试。 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. 听众接收新位置的频率太高,无法更新我的TextViev。 I solved it with a bit of synchronization, and skipping View refresh if it was being refreshed at that moment. 我通过一些同步解决了它,如果当时正在刷新,则跳过View刷新。

In your case, as you are not using a listener, I'd check for NullPointerExceptions . 在你的情况下,因为你没有使用监听器,我会检查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. 您可能没有将Android API用于Android模拟器,这就是为什么您无法访问GoogleMapAPı并遭遇类似崩溃的原因。

Check this for more info about setting emulator for usage with Maps. 有关设置模拟器以使用地图的详细信息,请查看此信息。

http://code.google.com/android/add-ons/google-apis/maps-overview.html 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. 这是因为您从模拟器中使用它并且它没有任何与gps相关的硬件。

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. 在Android模拟器中启动您的应用程序,并在SDK的/ tools目录中打开终端/控制台。 Connect to the emulator console: 连接到模拟器控制台:

telnet localhost <console-port>

Send the location data: geo fix to send a fixed geo-location. 发送位置数据:geo fix以发送固定的地理位置。 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. geo nmea发送NMEA 0183句子。 This command accepts a single NMEA sentence of type '$GPGGA' (fix data) or '$GPRMC' (transit data). 此命令接受单个“$ GPGGA”类型的NMEA语句(修复数据)或“$ GPRMC”(传输数据)。 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 来源: 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 另请参阅此链接: http//developer.android.com/guide/developing/devices/emulator.html#console

It is an API KEY issues. 这是一个API KEY问题。 You can try to request a new API KEY on google map API key. 你可以尝试申请一个新的API KEY在谷歌地图API密钥。
Your code is correct and didn't show any errors. 您的代码是正确的,没有显示任何错误。

Perhaps this answer can help you solve your problems. 也许这个答案可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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