简体   繁体   English

geocoder.getFromLocationName错误

[英]geocoder.getFromLocationName error

Getting "always true" result on if (location != null || !location.equals("")) and when I search something like "sldjfuhsdhfj" it closes the app, but when I enter something like "Central Park" it works correctly. if (location != null || !location.equals(""))上获得“始终为真”的结果,当我搜索“ sldjfuhsdhfj”之类的内容时,它将关闭该应用程序,但是当我输入“ Central Park”之类的内容时,它将起作用正确地。 What am I doing wrong? 我究竟做错了什么? Code I am using 我正在使用的代码

public void onMapSearch(View view) {
    EditText locationSearch = (EditText) findViewById(R.id.editText1);
    String location = locationSearch.getText().toString();
    List<Address> addressList = null;

    if (location != null || !location.equals("")) {
        Geocoder geocoder = new Geocoder(this);
        try {
            addressList = geocoder.getFromLocationName(location, 1);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
        mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 3s
                mMap.clear();
            }
        }, 3000);
    }
}

Address address = addressList.get(0) 地址地址= addressList.get(0)

You gave it junk so it is returning no results. 您给它加了垃圾,因此没有任何结果。 addressList is empty. addressList为空。 You are accessing the 0 element of something that is empty. 您正在访问空元素的0元素。 You should have seen a message in the Android Monitor window of Android Studio that pointed you to this exact line / issue. 您应该已经在Android Studio的“ Android监视器”窗口中看到一条消息,指出了此确切的线路/问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM