【发布时间】:2017-08-19 12:23:01
【问题描述】:
我只是想写一个返回纬度和经度的方法。我有一个带有按钮和两个文本字段的活动。我正在使用的java类扩展AppCompatActivity并实现LocationListener当按下按钮时,按下以下方法:
public void startGPS(View view)
{
// Here is the code to handle permissions - you should not need to edit this.
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION }, TAKE_PHOTO_PERMISSION);
}
Location location = locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, this);
}
稍后我尝试在我的 onLocationChanged 方法中打印出我的位置,但这导致应用程序崩溃。我通过调试器运行它,发现位置始终是null。
我尝试this solution,但没有成功。其他示例是在 onResume 中调用该函数,但我确实需要在 startGPS 方法中调用该函数。
另外,是否有可能只是我的设备出现错误?我在 Nexus 7 上运行它,运行 Google 地图时似乎没有任何问题。
【问题讨论】:
-
@AtefHares 是的,我已经把它们放在我的清单中了。
标签: java android android-studio gps android-gps