错误:(28,58)错误:不兼容的类型:对象无法转换为地址

时间:2017-06-21 15:06:18

标签: android geocode

我正在创建一个应用程序,它会显示来自给定字符串的替换和经度

Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
  List addressList = geocoder.getFromLocationName(locationAddress, 1);
  if (addressList != null && addressList.size() > 0) {
     Address address = addressList.get(0);
     StringBuilder sb = new StringBuilder();
     sb.append(address.getLatitude()).append("\n");
     sb.append(address.getLongitude()).append("\n");
     result = sb.toString();
  }
}

发生了这个异常:

Error:(28, 58) error: incompatible types: Object cannot be converted to Address
The List addressList is storing address type values .

1 个答案:

答案 0 :(得分:0)

这是因为您使用原始类型List。您需要使用List<Address>

调用以下代码时:

Address address = addressList.get(0);

它从以下代码返回对象

List addressList = geocoder.getFromLocationName(locationAddress, 1);

然后它会出错,因为addressList.get(0)是一个不是 instanceof 地址的对象。

详细了解原始类型:https://stackoverflow.com/a/2770692/4758255