2015-05-14 16 views
5

Czy mogę użyć LocationListener z LocationManager w fragmencie. Właściwie, kiedy go używam, daje mi błąd w tym wierszu lm.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0,0, (android.location.LocationListener) to); a kiedy nie rzuca 4th parametr do android.location.LocationListener daje mi błąd ..LocationListener w fragmencie

import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 


public class Speedometer extends Fragment implements LocationListener { 

    TextView txt; 
    public Speedometer(){} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.speedometer, container, false); 

     txt=(TextView)rootView.findViewById(R.id.speedometer); 

     LocationManager lm= (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, (android.location.LocationListener) this); 

     return rootView; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     if(location==null){ 
      txt.setText("-.- m/s"); 
     } 
     else { 
      float nCurrentSpeed=location.getSpeed(); 
      txt.setText(nCurrentSpeed+"m/s"); 
     } 
    } 
} 
+0

używanie bez obsady 'lm.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0,0, this);' – Kushal

+0

bez obsady daje mi błędu – Bisma

Odpowiedz

12

Chyba błędnie wprowadzone com.google.android.gms.location.LocationListener interfejs, który ma tylko jeden 1 metody abstrakcyjne onLocationChanged

powinieneś raczej wdrażać android.location.LocationListener że ma 4 metody abstrakcyjne

onLocationChanged(Location location),

onProviderDisabled(String provider),

onProviderEnabled(String provider),

onStatusChanged(String provider, int status, Bundle extras)

+0

Dziękuję bardzo, użyłem go i mój błąd został usunięty – Bisma

+0

Fajnie! To trochę zagmatwane, aby dowiedzieć się, kiedy użyć jednego lub drugiego ... – Supercelo

Powiązane problemy