ClassCastException mit LocationListener

  • Antworten:2
Benjamin F.
  • Forum-Beiträge: 4

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?

Antworten
swa00
  • Forum-Beiträge: 3.704

21.08.2017, 12:08:07 via Website

EDIT : Beitrag entfernt - Ludy war exakter :-)

— geändert am 21.08.2017, 16:30:37

Liebe Grüße - Stefan
[ App - Entwicklung ]

Antworten
Ludy
  • Admin
  • Forum-Beiträge: 7.958

21.08.2017, 13:35:40 via App

Hallo Benjamin,

hier ist dein Fehler:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, (LocationListener) ctx);

Warum wird hier aus einem Context ein LocationListener, wo holt ihr euch solche fatalen Beispiele her?

Schau hier wie es richtig gehen kann: https://github.com/Ludy87/AndroidPIT

Gruß Ludy (App Entwickler)

Mein Beitrag hat dir geholfen? Lass doch ein "Danke" da.☺

☕ Buy Me A Coffee ☕

Lebensmittelwarnung-App

✨Meine Wunschliste✨

📲Telegram NextPit News📲

Benjamin F.swa00

Antworten