我想检测我的位置,我使用ACCESS_FINE_LOCATION
许可,但是当我运行的应用程序,它崩溃并在屏幕上出现错误信息:如何在android中使用GPS获取位置?
“APPNAME”已停止工作
以下是我的代码,我用于实现上述说明:
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.v("My Coordinates are: ", location.getLatitude() + " " + location.getLongitude());
}
}
我在做什么错在这里?
请帮忙。
检查此[Android的GPS定位管理器(https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/) –
如果您使用的是Android 6 +,那么这可能是由于运行时权限造成的。您可以阅读接受的答案在https://stackoverflow.com/questions/40142331/how-to-request-location-permission-on-android-6。另外,如果你可以提供你的堆栈跟踪会更容易。 – BajajG
logcat中会有堆栈跟踪,它会告诉你更多关于这个问题的信息。检查https://stackoverflow.com/questions/23353173/unrible-myapp-has-stopped-how-can-i-solve-this/23353174#23353174 –