我添加列表时我的应用就会停止 代码中的listAddresses

问题描述 投票:-1回答:1

我正在编写代码以获取有关用户位置的信息。但是,当我添加以下代码时,我的应用程序停止运行,并且模拟器中仅出现白屏:

List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);

mMap.clear();
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));

Geocoder geocoder = new Geocoder(getApplicationContext(),Locale.getDefault());
try {
    List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(), 1);

    if (listAddresses != null && listAddresses.size() > 0) {
        String address = "";

关于可能是什么问题的任何想法?预先谢谢!

java android android-studio
1个回答
0
投票

我想到的许多事情是:

  1. geoCoder为空
  2. 位置为空
  3. 我建议用Try-Catch包装整个代码
  4. 添加日志,以便我们获得问题的更好图片
© www.soinside.com 2019 - 2024. All rights reserved.