Dan*_*ñez 2 android localization android-studio location-services
在我的应用程序中,在连接到LocationServices.API之后,在onConnected()回调中,我启动了一个位置Updates Request,问题是处理进程的Activity(DataActivity)无法识别为LocationListener的一个实例,它显示了一个错误,并建议投射.
protected void startLocationUpdates() {
PendingResult locationUpdatesResult = LocationServices.FusedLocationApi.requestLocationUpdates(locationGoogleApiClient, locationRequest, (com.google.android.gms.location.LocationListener) this);
}
Run Code Online (Sandbox Code Playgroud)
当我将最后一个参数转换为(com.google.android.gms.location.LocationListener)时,IDE允许我编译,但抛出ClassCastException
java.lang.ClassCastException:com.example.[...].无法将数据活动强制转换为com.google.android.gms.location.LocationListener
我真的不知道为什么会这样.
这是班级宣言.
public class DataActivity extends FragmentActivity implements LocationListener, ConnectionCallbacks , OnConnectionFailedListener, OnMapReadyCallback{
Run Code Online (Sandbox Code Playgroud)
OnCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
//Create the GoogleLocationAPI reference
buildGoogleLocationApiClient();
//Create Location Request
createLocationRequest();
//Initialize the mapFragment
initializeMapFragment();
updateUI();
}
Run Code Online (Sandbox Code Playgroud)
其他方法:
protected synchronized void buildGoogleLocationApiClient(){
locationGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
locationGoogleApiClient.connect();
Log.v("**INFO: ", "API CLIENT");
}
protected static void createLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
Log.i("**INFO: ", "LocationRequest created");
}
protected void initializeMapFragment(){
//Initialize the mapFragment
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
//When mapFragment is ready...
mapFragment.getMapAsync(this);
}
Run Code Online (Sandbox Code Playgroud)
因此,当建立连接(OnConnected)时,我会在错误弹出时调用此方法.
protected void startLocationUpdates() {
PendingResult locationUpdatesResult = LocationServices.FusedLocationApi.requestLocationUpdates(locationGoogleApiClient, locationRequest, this);
locationUpdatesResult.await();
}
Run Code Online (Sandbox Code Playgroud)
Ger*_*ero 16
确保你的DataActivity工具,com.google.android.gms.location.LocationListener而不是android.location.LocationListener.
您可能android.location.LocationListener错误地导入了.
| 归档时间: |
|
| 查看次数: |
8221 次 |
| 最近记录: |