2016-11-09 4 views
1

Jedes Mal, wenn ich die App öffne sagt es leider Ihre App wurde gestoppt. Ich bin nicht in der Lage, den FehlerGoogle Maps - Ich versuche den aktuellen Standort zu aktualisieren, aber meine App ist geschlossen, sobald ich sie öffne. Hier ist der Code-

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    protected LocationManager locationManager; 
    TextView tv1; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, (android.location.LocationListener) mLocationListener); 


    } 
    private final LocationListener mLocationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(final Location location) { 
      //your code here 
      tv1 = (TextView) findViewById(R.id.tv1); 
      tv1.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
     } 
    }; 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     mMap.setMyLocationEnabled(true); 
    } 
} 

Hier wird das Protokoll Fehler zu finden -

java.lang.RuntimeException: Kann Aktivität ComponentInfo starten {manishsaran.maper/manishsaran.maper.MapsActivity }: java.lang.ClassCastException: manishsaran.maper.MapsActivity $ 1 kann nicht

+0

ich die Lösung gefunden und Das Problem war, dass ich LocationManager (com.google.android.gms.location) anstelle von android.location.LocationListener –

Antwort

1

Ausgabe zu android.location.LocationListener gegossen werden soll, Ihre Location sollten Sie es durch

import android.location.LocationListener; 

importieren und seine ganze Methode wie folgt implementieren:

private final LocationListener mLocationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(final Location location) { 
     //your code here 
     tv1 = (TextView) findViewById(R.id.tv1); 
     tv1.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 
}; 

Und es gibt keine Notwendigkeit, Ihre Location Gießen:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, mLocationListener); 
+0

implementiert habe Ich fand die Lösung und das Problem wurde implementiert d LocationManager (com.google.android.gms.location) anstelle von android.location.LocationListener –

+0

@AvinashSaran Großartig, überprüfen Sie immer die Importe und Klassen. –

Verwandte Themen