【问题标题】:LocationListener in fragment片段中的 LocationListener
【发布时间】:2015-05-14 09:00:11
【问题描述】:

我可以在片段中使用 LocationListener 和 LocationManager 吗?实际上,当我使用它时,它给了我这一行的错误:

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

当我不将第 4 个参数转换为 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,this);
  • 没有强制转换它会给我错误

标签: android android-fragments location


【解决方案1】:

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

你应该实现android.location.LocationListener,它有4个抽象方法

onLocationChanged(Location location),

onProviderDisabled(String provider),

onProviderEnabled(String provider),

onStatusChanged(String provider, int status, Bundle extras)

【讨论】:

  • 非常感谢,我用了它,我的错误被删除了
  • 酷!弄清楚何时使用其中一个有点令人困惑......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 2014-06-26
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多