【问题标题】:I have trouble in SearchView Google Maps我在 SearchView Google Maps 中遇到问题
【发布时间】:2021-05-25 03:47:04
【问题描述】:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                String location = searchView.getQuery().toString();
                //List<Address> addressList = new ArrayList<>();
                List<Address> addressList = null;
                if(location != null || !location.equals("")){
                    Geocoder geocoder = new Geocoder(MapsActivity.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(location));
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
        

它在 AVD 模拟器上运行,但在我的手机上运行不正常。我搜索的东西和崩溃有错误“

E/AndroidRuntime: 致命异常: main 进程:com.example.coffeebakeryfordriver,PID:27228 java.lang.IndexOutOfBoundsException:索引:0,大小:0 在 java.util.ArrayList.get(ArrayList.java:437) 在 com.example.coffeebakeryfordriver.MapsActivity$2.onQueryTextSubmit(MapsActivity.java:96) 在 android.widget.SearchView.onSubmitQuery(SearchView.java:1301) 在 android.widget.SearchView.access$1000(SearchView.java:101) 在 android.widget.SearchView$7.onEditorAction(SearchView.java:1278) 在 android.widget.TextView.onEditorAction(TextView.java:7042) 在 com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138) 在 com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:357) 在 com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:89) 在 android.os.Handler.dispatchMessage(Handler.java:107) 在 android.os.Looper.loop(Looper.java:224) 在 android.app.ActivityThread.main(ActivityThread.java:7551) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995) I/Process:发送信号。 PID:27228 SIG:9

"

【问题讨论】:

  • addressList 为空添加所有功能到 try
  • 还没有。我尝试在 SS Snapdragon 上构建没问题,但在小米联发科上构建不起作用

标签: java android maps searchview


【解决方案1】:

在你的类中使用这个方法并在 onReadyMap() 方法中调用该方法,这对我来说很好。

private void searchMethod(){

    // adding on query listener for our search view.
    binding.idSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            // on below line we are getting the
            // location name from search view.
            String location = binding.idSearchView.getQuery().toString();

            // below line is to create a list of address
            // where we will store the list of all address.
            List<Address> addressList = null;

            // checking if the entered location is null or not.
            if (location != null || location.equals("")) {
                // on below line we are creating and initializing a geo coder.
                Geocoder geocoder = new Geocoder(MapsActivity.this);
                try {
                    // on below line we are getting location from the
                    // location name and adding that location to address list.
                    addressList = geocoder.getFromLocationName(location, 1);

                } catch (IOException e) {
                    e.printStackTrace();
                }
                // on below line we are getting the location
                // from our list a first position.
                Address address = addressList.get(0);

                // on below line we are creating a variable for our location
                // where we will add our locations latitude and longitude.
                LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());

                // on below line we are adding marker to that position.
                mMap.addMarker(new MarkerOptions().position(latLng).title(location));

                // below line is to animate camera to that position.
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
            }
            return false;

        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2019-06-28
    • 2020-02-21
    • 2021-05-19
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    相关资源
    最近更新 更多