我可以在片段中將LocationListener與LocationManager一起使用嗎?實際上,當我使用它,它給了我在這行錯誤lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,(android.location.LocationListener)this);當我不蒙上第四參數android.location.LocationListener它給我的錯誤..片段中的位置監聽器
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");
}
}
}
使用不投'lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,這一點);' – Kushal
不投它給了我錯誤 – Bisma