私はjavaでのプログラミングや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)を参照してください。これがあなたの問題を解決するのに役立つことを願っています。 –