首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >片段中的LocationListener

片段中的LocationListener
EN

Stack Overflow用户
提问于 2015-05-14 17:00:11
回答 1查看 9.4K关注 0票数 7

我可以在片段中使用带有LocationManager的LocationListener吗?实际上,当我使用它时,它会给出下面这一行的错误:

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, (android.location.LocationListener) this);

当我没有将第四个参数强制转换为android.location.LocationListener时,它会给我错误...

代码语言:javascript
复制
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");
        }   
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-15 17:32:37

我认为您错误地实现了只有一个抽象方法onLocationChangedcom.google.android.gms.location.LocationListener接口

您应该实现具有4个抽象方法的android.location.LocationListener

onLocationChanged(Location location)

onProviderDisabled(String provider)

onProviderEnabled(String provider)

onStatusChanged(String provider, int status, Bundle extras)

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30233219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档