【问题标题】:java.util.arraylist cannot be cast to android.location.addressjava.util.arraylist 不能转换为 android.location.address
【发布时间】:2013-07-11 23:16:49
【问题描述】:

我试图将所有地址(字符串)放入一个列表中,然后一个一个地获取它们并使用标记填充地图,但我收到此错误,因为 java.util.arraylist 无法强制转换为 android.location.address .有什么帮助吗?

这是产生错误的代码片段

            int i = 0;
            List<List<Address>> addressList = new ArrayList<List<Address>>();
            //while (indirizzi != null) {
            while (i <= 3) {
                try {

                    addressList.add(geocoder.getFromLocationName(indirizzi.get(i), 1));
                    Log.i("indirizzo i-esimo",indirizzi.get(i));
                    i++;
                } catch (IOException e) {
                    Log.i("geolocation","geolocation IOException");
                    e.printStackTrace();
                }
            }

            for (int j = 0; j < addressList.size(); j++) {

                Address address = (Address) addressList.get(j);
                if(address.hasLatitude() && address.hasLongitude()){
                    latLng = new LatLng(address.getLatitude(), address.getLongitude());
                }

                markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                markerOptions.title(indirizzi.get(i));

                markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icn_albero));

                Log.i("for", Integer.toString(i));
                j++;
                googleMap.addMarker(markerOptions);
            }

【问题讨论】:

    标签: android geolocation google-maps-markers


    【解决方案1】:

    看定义,是ListListAddressList

    List<List<Address>> addressList = new ArrayList<List<Address>>();
    

    你不能这样做:

    Address address = (Address) addressList.get(j);
    

    因为这会给你一个List&lt;Address&gt;,它不是Address 对象。

    1. 你可以这样做:

      Address address = (Address) addressList.get(j).get(someOtherIndex);
      
    2. List 定义为:

      List<Address> addressList = new ArrayList<Address>();
      

    【讨论】:

    • 我已经尝试了您向我展示的 2 方法,但随后我必须转换此 addressList.add((Address) geocoder.getFromLocationName(indirizzi.get(i), 1));它仍然不起作用!并使用第一种方法,我得到一个索引越界错误!
    【解决方案2】:

    我已经使用了下面的代码。

    我已经从 oncreate 中调用了 GeocoderTask 类。

    ed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
              @Override
              public void onItemClick(AdapterView<?> parent, final View view,
                  int position, long id) {
                addresstext = (String) parent.getItemAtPosition(position);
    
                Toast.makeText(getBaseContext(),""+addresstext+ "", Toast.LENGTH_SHORT).show();
    
                if(addresstext!=null && !addresstext.equals("")){
                    new GeocoderTask().execute(addresstext);
                }
              }
    
            });
    

    -

    private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
    
     @Override
     protected List<Address> doInBackground(String... locationName) {
         // Creating an instance of Geocoder class
         Geocoder geocoder = new Geocoder(getBaseContext());
         List<Address> addresses = null;
    
         try {
             // Getting a maximum of 10 Address that matches the input text
             addresses = geocoder.getFromLocationName(locationName[0], 10);
             System.out.println(" Inside Background Process");
         } catch (IOException e) {
             e.printStackTrace();
         }
         return addresses;
     }
    
    
     @Override
     protected void onPostExecute(List<Address> addresses) {
    
         if(addresses==null || addresses.size()==0){
             Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
         }
    
         // Clears all the existing markers on the map
         if(marker!=null){
         marker.remove();
         }
    
         // Adding Markers on Google Map for each matching address
         for(int i=0;i<addresses.size();i++){
    
    
             Address address = (Address) addresses.get(i);
             // Creating an instance of GeoPoint, to display in Google Map
             LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
    
             addressText = String.format("%s, %s",
             address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
             address.getCountryName());
    
             System.out.println(" Inside OnPostExecutemeathod Process");
    
             Toast.makeText(getBaseContext(), ""+address.getLatitude()+" - "+address.getLongitude()+"", Toast.LENGTH_SHORT).show();
    
             CameraPosition cameraPosition = new CameraPosition.Builder()
             .target(latLng)      // Sets the center of the map to Mountain View
             .zoom(17)                   // Sets the zoom
             .bearing(90)                // Sets the orientation of the camera to east
             .tilt(30)                   // Sets the tilt of the camera to 30 degrees
             .build();                   // Creates a CameraPosition from the builder
         map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
          marker = map.addMarker(new MarkerOptions()
           .position(latLng)
           .title(""+addresstext+""));
    
          marker.showInfoWindow();
         }
    
     }
    

    【讨论】:

      猜你喜欢
      • 2020-12-20
      • 2023-03-29
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 2013-03-24
      相关资源
      最近更新 更多