Android GPS位置在设备上为空,但在模拟器上工作
我正在我的设备上测试以下代码,它给了我Android GPS位置在设备上为空,但在模拟器上工作,android,Android,我正在我的设备上测试以下代码,它给了我位置对象空。gps在我的设备上被激活,但即使如此,它也没有给出我的位置 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lm = (LocationManager) getSy
位置
对象空
。gps在我的设备上被激活,但即使如此,它也没有给出我的位置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (l==null) {
Toast.makeText(getBaseContext(), "Location is null", 300).show();
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.geocoding"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.geocoding.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请提供帮助:-)您的代码假定创建位置已知。大多数时候,这是错误的。正确的代码应该将LocationListener注册到LocationManager,并等待一段时间以获得回复。时间每次都可能不同,这取决于你在等待什么样的位置。将位置移到onResume()可能会有所帮助,也许那个时候,它已经有了更高的可用性。但是,如果应用程序值得使用,请使用异步调用并等待您的回复
请参见它可以返回null,因此您应该在线程中轮询它。GPS可能已连接,但如果您在建筑物内,则不会更新位置。您正在从最后一个已知位置获取位置,而不是使用it实时。这可能会有问题,因为如果你在阴天改变位置,它将仍然是最后一个已知的位置。如果此项为空且是否符合您的需要,您可以包含来自
网络\u提供商的检查:)如果我尝试标准c=new Criteria();l=lm.getLastKnownLocation(lm.getBestProvider(c,false))代码>这会更好吗?它在模拟器上工作吗..如果它在设备上工作而不工作,则意味着您的设备没有得到gps定位。或者您可以尝试@Usman建议的方法。在您的手机之前收到gps定位后,它可能不为空。试着用谷歌地图打开你的手机(GPS也打开了),然后等到你有了GPS定位(不要在室内这样做,因为在建筑物内很难找到GPS定位)。在这之后,您可能会有一些不同的东西在这里为null。顺便说一句:根据文件,如果设备上没有打开GPS,它也会返回null。。。