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

Android-尝试在模拟器上测试GPS时出错,android,Android,我想测试一下在android上使用GPS的简单简介。所以我写了一个简单的代码如下: public class main extends Activity{ LocationManager mLocationManager; TextView tv; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);

我想测试一下在android上使用GPS的简单简介。所以我写了一个简单的代码如下:

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());
}
    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
}

另外,在AndroidManifest上,我添加了如下使用权限:

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());
}
    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


但当我尝试在android emulator上运行时,我得到一个错误“意外停止”,这个应用程序关闭了。我还添加了一些使用DDMS的尝试。但是我不知道这个错误。

你可能没有在Android模拟器上使用Google API,这就是为什么你不能访问Google Map APı并得到这样的崩溃

有关将emulator设置为与地图一起使用的更多信息,请选中此项


您可能没有在Android模拟器上使用Google API,这就是为什么您无法访问Google Map APı并获得这样的崩溃

有关将emulator设置为与地图一起使用的更多信息,请选中此项


我在实际设备中也遇到过类似的问题。侦听器接收新位置的频率太高,无法更新我的TextViev。我通过一点同步解决了这个问题,如果当时正在刷新视图,则跳过视图刷新


在您的情况下,由于您没有使用侦听器,我会检查
NullPointerExceptions

我在实际设备中也遇到过类似的问题。侦听器接收新位置的频率太高,无法更新我的TextViev。我通过一点同步解决了这个问题,如果当时正在刷新视图,则跳过视图刷新


在您的情况下,由于您没有使用侦听器,我会检查
NullPointerExceptions

这是一个
API键
问题。您可以尝试在google map API KEY上请求新的
API KEY

您的代码正确,没有显示任何错误


也许这个答案可以帮助您解决问题。

这是一个
API密钥问题。您可以尝试在google map API KEY上请求新的
API KEY

您的代码是正确的,没有显示任何错误


也许这个答案可以帮助您解决问题。

这是因为您使用的是模拟器,它没有任何gps相关硬件

这里有一个解决方案

要从命令行发送模拟位置数据,请执行以下操作:

在Android emulator中启动应用程序,并在SDK的/tools目录中打开终端/控制台。 连接到emulator控制台:

telnet localhost <console-port>
geo nmea发送nmea 0183语句。 此命令接受单个NMEA语句,类型为“$GPGGA”(修复数据)或“$GPRMC”(传输数据)。例如:

geo fix -121.45356 46.51119 4392
geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
资料来源:


另请参见此链接:

这是因为您从模拟器使用它,而它没有任何gps相关硬件

这里有一个解决方案

要从命令行发送模拟位置数据,请执行以下操作:

在Android emulator中启动应用程序,并在SDK的/tools目录中打开终端/控制台。 连接到emulator控制台:

telnet localhost <console-port>
geo nmea发送nmea 0183语句。 此命令接受单个NMEA语句,类型为“$GPGGA”(修复数据)或“$GPRMC”(传输数据)。例如:

geo fix -121.45356 46.51119 4392
geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
资料来源:


另请参见此链接:

logcat怎么说?碰撞发生在哪一行?在这里检查logcat是怎么说的?崩溃发生在哪一行?检查此处获取GPS修复不需要Google Map API。获取GPS修复不需要Google Map API。问题解决。thans对于@Mister smith,我添加了这样的排序代码,如果(location!=nul){}否则{}谢谢mate:)解决问题。thans对@Mister smith我添加了这样的排序代码,如果(location!=nul){}否则{}谢谢mate:)我知道模拟器和设备。但肯定不是你在考虑什么。无论如何,感谢您的回答@Aruni了解模拟器和设备。但肯定不是你在考虑什么。无论如何,谢谢你的回答@Arun