저는 자바 프로그래밍과 Android 프로그래밍에서 새로운데, 내 위치를 얻으려고하고 있지만 몇 가지 문제가 있습니다. Android에서 위치를 가져 오는 데 문제가 발생했습니다.
이
내가 는 코드if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this);
의이 부분에서
경우 문장에 도달했을 때 나는 그것이 충돌 디버깅에 의해 발견
public class MiServicio extends Service implements LocationListener{private final Context context;
double latitud;
double longitud;
Location location;
boolean gpsActivo;
TextView texto;
LocationManager locationManager;
public MiServicio() {
super();
this.context = this.getApplicationContext();
}
public MiServicio(Context c) {
super();
this.context = c;
getLocation();
}
public void setView(View v) {
texto = (TextView) v;
texto.setText("Coordenadas: " + latitud + ", " + longitud);
}
public void getLocation() {
try {
locationManager = (LocationManager) this.context.getSystemService(LOCATION_SERVICE);
gpsActivo = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ignored) {
}
if (gpsActivo) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
latitud = location.getLatitude();
longitud = location.getLongitude();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}을 작성한 모든 코드
누군가이 문제를 해결하는 방법을 설명 할 수 있습니까?
감사합니다.
또한 여기에 내가 매니페스트
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
편집에서 설정 한 권한
These are all the errors I got
는 또한 내가
어떤 오류가 있습니까? logcat – tahsinRupam
에뮬레이터 란 무엇입니까? 또는 장치 위치가 켜져 있습니까? –
[이 링크] (http://stackoverflow.com/questions/37322645/nullpointerexception-when-trying-to-check-permissions)를 참조하십시오. 귀하의 문제를 해결하는 데 도움이되기를 바랍니다. –