无法解析方法'requestLocationUpdates

问题描述:

我最近开始使用Android开发,并希望应用程序显示经度和纬度值。但是,getLastKnowLocation()总是返回null,所以我尝试使用requestLocationUpdate(),但是出现错误。这里是我的代码:无法解析方法'requestLocationUpdates

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    double longitude = location.getLongitude(); 
    double latitude = location.getLatitude(); 


public final LocationListener locationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
     if(location!=null){ 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
     } 

      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,10,locationListener); 
    } 

}; 

这里是我的进口:

import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 

import com.google.android.gms.location.LocationListener; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.text.DateFormat; 
import java.util.Date; 
import java.util.Locale; 

而我使用的许可:

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

更新的代码:

public final LocationListener locationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 
     } 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
    } 

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

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 

}; 

你使用错误的LocationListener。您需要导入android.location.LocationListener

+0

嗨,非常感谢您的回答。但是现在,'new LocationListener()'得到了这个错误:_Class'LocationListener派生的匿名类'必须被声明为抽象或实现'LocationListener'_ – whzeng

+0

'onStatusChanged(String,int,Bundle)'抽象方法好吧,两个LocationListeners是完全独立的类,并且您需要提供不同的方法。 – ianhanniballake

+0

请看看我更新的代码。我正确地做这件事吗? “this”有错误,说这是_错误的第一个参数type_ – whzeng