- Forum-Beiträge: 4
21.08.2017, 11:57:06 via Website
21.08.2017 11:57:06 via Website
Hey Leute
Ich versuche gerade meinen Standort heraus zu bekommen. Beim initialisieren des locationManager zeigt er mir in einer Exception folgendes an: "... MainActivity cannot be cast to android.location.LocationListener".
Meine erstellte Klasse:
`import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
public class GPStracker implements LocationListener {
private static Context ctx;
private static LocationManager locationManager;
private static double latitude;
private static double longitude;
public GPStracker(Context ctxOri) {
ctx = ctxOri;
locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(ctx, 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;
}
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, (LocationListener) ctx);
}catch (Exception e){
Toast.makeText(ctx, e.toString(), Toast.LENGTH_SHORT).show();
}
}
public static double getLatitude(){
return latitude;
}
public static double getLongitude(){
return longitude;
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(ctx, "Gps is turned on!! ",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
ctx.startActivity(intent);
Toast.makeText(ctx, "Gps is turned off!! ",
Toast.LENGTH_SHORT).show();
}
}
`
Und hier wird es in einem Fragment aufgerufen:
ivGBPLoacation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GPStracker gpst = new GPStracker(getContext());
Toast.makeText(getContext(), gpst.getLatitude() + " ; " + gpst.getLongitude(), Toast.LENGTH_SHORT).show();
}
});
Wie gesagt bekomme ich die jeweilige Exception und er zeigt im Toast die Koordinaten 0,0 ; 0,0 an.
Es wird warscheinlich an dem (LocationListener) ctx liegen im Konstruktor. Muss ich das bei Fragments anders lösen oder woran liegt es?
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.