2012-06-27 64 views
0

我在google地圖上顯示POI的列表: -WHERE ItemizeOverlay產生ArrayOutOfBoundException不總是發生此問題?

1-用戶放大或縮小webservice調用,它響應特定位置的xml屬性。

2-獲得屬性後,我們必須過濾那些具有相同經緯度的屬性,並在地圖上顯示氣泡上的這些數字(例如,3列表具有相同的緯度,將該列表放入數組列表並顯示在氣泡3上) 。

3-但當我很少並不總是我縮放地圖以下發生異常:

06-27 15:10:57.269: E/AndroidRuntime(4968): FATAL EXCEPTION: main 
    06-27 15:10:57.269: E/AndroidRuntime(4968): java.lang.ArrayIndexOutOfBoundsException: index=0 length=0 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at com.google.android.maps.ItemizedOverlay.getIndexToDraw(ItemizedOverlay.java:211) 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240) 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at com.google.android.maps.Overlay.draw(Overlay.java:179) 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at com.google.android.maps.MapView.onDraw(MapView.java:530) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.View.draw(View.java:9304) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.drawChild(ViewGroup.java:2586) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.drawChild(ViewGroup.java:2584) 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.drawChild(ViewGroup.java:2584) 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.drawChild(ViewGroup.java:2584) 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
    06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.drawChild(ViewGroup.java:2584) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.drawChild(ViewGroup.java:2584) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.View.draw(View.java:9307) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.widget.FrameLayout.draw(FrameLayout.java:419) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2076) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewRoot.draw(ViewRoot.java:1706) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewRoot.performTraversals(ViewRoot.java:1420) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.view.ViewRoot.handleMessage(ViewRoot.java:2066) 
06-27 15:10:57.269: E/AndroidRuntime(4968): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-27 15:10:57.269: E/AndroidRuntime(4968):  at android.os.Looper.loop(Looper.java:132) 
06-27 15:10:57.269: E/AndroidRuntime(4968):  at android.app.ActivityThread.main(ActivityThread.java:4126) 
06-27 15:10:57.269: E/AndroidRuntime(4968):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-27 15:10:57.269: E/AndroidRuntime(4968):  at java.lang.reflect.Method.invoke(Method.java:491) 
06-27 15:10:57.269: E/AndroidRuntime(4968):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 
06-27 15:10:57.269: E/AndroidRuntime(4968):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
06-27 15:10:57.269: E/AndroidRuntime(4968):  at dalvik.system.NativeStart.main(Native Method) 

,我使用此代碼:-`

mapView.setOnTouchListener(new OnTouchListener() { 

private class Mapdatatask extends AsyncTask<Void, Void, Integer> { 

    @Override 
    protected Integer doInBackground(Void... params) { 

     try 
     { 
      StringBuilder sb=new StringBuilder(); 
      URL sourceUrl=new URL(ListURL); 
      System.out.println("----ListURL------"+ListURL); 
      InputStream inputStream=sourceUrl.openStream(); 
      //add if necessary the PushBack measure to safeguard the intended purpose here if really happening 
      //that you can actually ascertain it by saving a local copy for inspection 
      BufferedReader br=new BufferedReader(new InputStreamReader(inputStream)); 
      String line; 
      while ((line=br.readLine())!=null) { 
       sb.append(line+"\n"); 
      } 
      br.close(); 

      SAXParserFactory spff=SAXParserFactory.newInstance(); 
      spff.setNamespaceAware(true); 
      SAXParser spp=spff.newSAXParser(); 
      XMLReader xr=spp.getXMLReader(); 
      myHandler=new ApplicationHandler(); 
      xr.setContentHandler(myHandler); 
      xr.parse(new InputSource(new StringReader(sb.toString()))); 
      //xr.parse(new InputSource(sourceUrl.openStream())); 
      valueoftotal = ApplicationHandler.value; 


     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } catch (SAXException e) { 

      try 
      { 
       StringBuilder sb=new StringBuilder(); 
       URL sourceUrl=new URL(ListURL); 
       InputStream inputStream=sourceUrl.openStream(); 
       //add if necessary the PushBack measure to safeguard the intended purpose here if really happening 
       //that you can actually ascertain it by saving a local copy for inspection 
       BufferedReader br=new BufferedReader(new InputStreamReader(inputStream)); 
       String line; 
       while ((line=br.readLine())!=null) { 
        sb.append(line+"\n"); 
       } 
       br.close(); 
       inputStream.close(); 
       SAXParserFactory spff=SAXParserFactory.newInstance(); 
       spff.setNamespaceAware(true); 
       SAXParser spp=spff.newSAXParser(); 
       XMLReader xr=spp.getXMLReader(); 
       Log.e("ListURL to parse ",""+ListURL); 

       myHandler=new ApplicationHandler(); 
       xr.setContentHandler(myHandler); 
       System.out.println(""+sb); 
       xr.parse(new InputSource(new StringReader(sb.toString()))); 
       //xr.parse(new InputSource(sourceUrl.openStream())); 
       valueoftotal = ApplicationHandler.value; 
       //arrayList = myHandler.getParsedData(); 

      } 
      catch(IOException e1) { 
       e1.printStackTrace(); 
       Log.e("parse exception0"," dont know why"); 
       Log.e("XML Error ",e.toString()); 

     return 1; 

    } 


    @Override 
    protected void onPostExecute(Integer result) 
    { 
     if(valueoftotal!=null && !valueoftotal.equalsIgnoreCase("")) 
     { 
      if(Integer.parseInt(valueoftotal) > 0 && Integer.parseInt(valueoftotal) <= 100) 
      { 
       OverlayItem oi=null; 
       if(myHandler.getParsedData()!=null) 
       { 
        ArrayList<Applicationdataset> arrayList = myHandler.getParsedData(); 
        //mlist = new ArrayList<Applicationdataset>(); 
        myvectorlist = new Vector<ArrayList<Applicationdataset>>(); 
        //mlist.addAll(arrayList); 
        System.out.println(arrayList.size()+"++++++++++"); 
        Map<Key, ArrayList<Applicationdataset>> mlistVector = groupTheList(arrayList); 
        myvectorlist = putIntoVector(mlistVector); 
        for(int mindex=0;mindex<myvectorlist.size();mindex++) 
        { 
         if(myvectorlist.get(mindex)==null) 
         { 
          myvectorlist.remove(mindex); 
         } 
         if(myvectorlist.get(mindex).size()==0) 
         { 
          myvectorlist.remove(mindex); 
         } 
        } 
       } 
       linearbottom2.setClickable(true); 
       synchronized (oi) { 
        mapOverlays.clear(); 
        overlay.removeAll(); 
       } 

       lineartabbar.setBackgroundResource(R.drawable.mapbar); 

       if(myvectorlist != null && myvectorlist.size() > 1) 
       { 
        Applicationdataset data = null; 
        for(int i = 0; i < myvectorlist.size(); i++) 
        { 
         { 
          data = myvectorlist.get(i).get(0); 
          String latvalue = data.getLatitude().toString(); 
          String lonvalue = data.getLongitude().toString(); 

          GeoPoint point = null; 
          try 
          { 
           point = new GeoPoint((int)(Double.parseDouble(latvalue)*1E6),(int)(Double.parseDouble(lonvalue)*1E6)); 
          } 
          catch (NumberFormatException e) 
          { 
          } 

          if(point != null) 
          { 
           oi = overlay.createItem(i); 
           if(overlay!=null) 
           { 
            if(oi!=null) 
            { 
             System.out.println("overlay.size()--"+overlay.size()); 
            if(overlay.size()!=0) 
            { 
             overlay.addItem(oi); 

             if(myvectorlist.size() == 1) 
             { 
              mc.animateTo(point); 
              mc.setCenter(point); 
              mc.setZoom(16); 
             } 
            } 
            } 

           }//end 
          } 

        } 

        System.out.println("+++size of overlay++++"+overlay.size()); 

        mapOverlays.add(overlay); 

        mapView.invalidate(); 
       } 


       textViewpopup.setText(valueoftotal+" listings found."); 
       linearlayoutpopup.setVisibility(View.VISIBLE); 
       RunAnimationslisting(); 

      } 
      else if(Integer.parseInt(valueoftotal) > 100) 
      { 

       Eyelidmessage(valueoftotal+" listings found. \n Zoom-in, press the locate button below or select the refine button above to display fewer properties. "); 
       lineartabbar.setBackgroundResource(R.drawable.mapwithoutlist); 
       linearbottom2.setClickable(false);     

      } 
      else if(Integer.parseInt(valueoftotal) == 0) 
      { 
       GoneAnimations(); 
       lineartabbar.setBackgroundResource(R.drawable.mapwithoutlist); 
       linearbottom2.setClickable(false); 
       customizeDialog = new CustomizeDialog(activity); 
       customizeDialog.setTitle("Sorry"); 
       customizeDialog.setMessage("No Listing Found"); 
       customizeDialog.show(); 
      } 
     } 

     super.onPostExecute(result); 
    } 

    @Override 
    protected void onCancelled() { 
     currenttask = null; 
    } 

} 

private class GoogleMapViewOverlay extends ItemizedOverlay 
{ 
    ImageButton bluebutton,savebutton,closebutton; 

    ImageThreadLoader imageloader; 

    //final MapController mc; 
    TextView text1,text2,text3,text4,textup; 
    ImageView imageview; 

    private PopupPanel panel = new PopupPanel(R.layout.mappopup); 
    private Drawable marker; 
    private Context mContext; 


    public GoogleMapViewOverlay(Drawable drawable, MapView mapView, 
      Activity activity2) 
    { 
     //super(drawable); 
     super(boundCenterBottom(drawable)); 
     setLastFocusedIndex(-1); 
     populate();   

     items = new ArrayList<OverlayItem>(); 
     marker = drawable; 
     mContext = mapView.getContext(); 
     mc = mapView.getController(); 
     this.activity = activity2; 
     DB = new DatabaseHelper(activity2); 
    } 

    public void removeAll() 
    { 
     items.clear(); 
     setLastFocusedIndex(-1); 
     populate(); 
    }  

    @Override 
    protected OverlayItem createItem(int index) 
    { 
     Projection getcoords; 
     GeoPoint point = null; 
     int size =0; 
     String latvalue=""; 
     String lonvalue=""; 
     Point pcon = new Point(0,0); 
     getcoords = mapView.getProjection(); 

     if(myvectorlist!=null) 
     { 
      if(myvectorlist.size()!=0) 
      { 
      size = myvectorlist.get(index).size(); 
      latvalue = myvectorlist.get(index).get(0).getLatitude().toString(); 
      lonvalue = myvectorlist.get(index).get(0).getLongitude().toString();  
      } 
     } 

     try 
     { 
      point = new GeoPoint((int)(Double.parseDouble(latvalue)*1E6),(int)(Double.parseDouble(lonvalue)*1E6)); 
     } 
     catch (NumberFormatException e) 
     { 
      e.printStackTrace(); 
     } 

     if(getcoords!=null) 
     { 
      if(point!=null) 
      { 
       if(pcon!=null) 
       { 
        getcoords.toPixels(point, pcon);   
       }     
      }    
     } 

     OverlayItem oi = new OverlayItem(point,"", ""); 
     if(size == 1) 
     { 
      boundCenterBottom(drawable);     
      oi.setMarker(drawable); 
     }else 
     if(size>1) 
     { 
      Drawable d= writeOnDrawable(drawable1,size); 
      boundCenterBottom(d);//drawable1     
      oi.setMarker(d);//drawable1 
     } 


     return oi; 
    } 

    @Override 
    public int size() 
    { 
     //Log.e("","items.size()"+items.size()); 
     if (items != null && items.size() > 0) 
      return items.size(); 
     else 
      return 0;  
    } 

    public void addItem(OverlayItem item) 
    { 
     System.out.println("adddoverlayitem "+items+"\titem"+item); 
     try { 

      items.add(item); 
      setLastFocusedIndex(-1); 
      populate(); 
     } catch (NullPointerException e) { 

      e.printStackTrace(); 
     } 
    } 

class PopupPanel 
    { 
    // View popup; 
    PopupPanel(int layout) 
     { 
     ViewGroup parent=(ViewGroup)mapView.getParent(); 
     popup = getLayoutInflater().inflate(layout, parent, false); 
     } 
    View getView() { 
     return(popup); 
    } 

    void show(boolean alignTop) 
    { 
     popup.setVisibility(View.VISIBLE); 
     RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT 
     ); 

     if (alignTop) 
     { 
      lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);    

      lp.setMargins(65, 100, 0, 0); 

     } 
     else 
     { 
      lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
      lp.setMargins(0, 0, 0, 60); 
     } 

     hide(); 

     ((ViewGroup)mapView.getParent()).addView(popup, lp);    

     isVisible=true; 

    } 

    void hide() { 

     if (isVisible) 
     { 
      isVisible=false; 
      ((ViewGroup)popup.getParent()).removeView(popup); 
     } 
    } 
} 

public void Eyelidmessage(String message) 
{ 
    textViewpopup.setText(message); 
    linearlayoutpopup.setVisibility(View.VISIBLE);    
    RunAnimations(); 
} 

private void RunAnimationslisting() { 

    Animation a; 
    a = AnimationUtils.loadAnimation(this, R.anim.translate); 
    a.reset(); 

    linearlayoutpopup.clearAnimation(); 
    a.setFillAfter(true); 
    linearlayoutpopup.startAnimation(a); 

    a.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      System.out.println("@@doneanimation listner works [email protected]@"); 

      try { 
       Thread.sleep(2000); 
       GoneAnimationslisting(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

    }); 

} 

private void GoneAnimationslisting() { 

    Animation a1; 
    a1 = AnimationUtils.loadAnimation(this, R.anim.translate1); 
    a1.reset(); 

    linearlayoutpopup.clearAnimation(); 
    a1.setFillAfter(true); 
    linearlayoutpopup.startAnimation(a1); 


    a1.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      System.out.println("@@doneanimation listner works [email protected]@"); 

      linearlayoutpopup.setVisibility(View.GONE); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 
    }); 
} 

private Drawable writeOnDrawable(Drawable objdrawable, int size) 
{ 
    String text = ""+size; 
    Bitmap bitmap = ((BitmapDrawable)objdrawable).getBitmap().copy(Bitmap.Config.ARGB_8888, true); 

    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL); 
    paint.setColor(Color.WHITE); 
    paint.setTypeface((Typeface.defaultFromStyle(Typeface.BOLD))); 
    paint.setTextSize(20); 
    float txtwidth = paint.measureText(text); 

    Canvas canvas = new Canvas(bitmap); 

    // canvas.drawText(text, 0, , paint); 
    canvas.drawText(text,((bitmap.getWidth()/2)-(txtwidth/2)),bitmap.getHeight()/2, paint); 

    Drawable d =new BitmapDrawable(getResources(),bitmap); 
    return d; 
} 


private Map<Key, ArrayList<Applicationdataset>> groupTheList(ArrayList<Applicationdataset> arrayList) { 
    Map<Key, ArrayList<Applicationdataset>> map = new HashMap<Key, ArrayList<Applicationdataset>>(); 
    for (Applicationdataset appSet : arrayList) { 
     Key key = new Key(appSet.getLatitude(), appSet.getLongitude()); 
     ArrayList<Applicationdataset> list = map.get(key); 
     if (list == null) { 
      list = new ArrayList<Applicationdataset>(); 
      map.put(key, list); 
     } 
     list.add(appSet); 
    } 
    return map; 
} 

public class Key { 
    String _lat; 
    String _lon; 

    Key(String lat, String lon) { 
     _lat = lat.trim(); 
     _lon = lon.trim(); 
    } 

    @Override 
    public boolean equals(Object o) { 
     //if (this == o) return true; 
     // if (o == null || getClass() != o.getClass()) return false; 

     //Key key = (Key) o; 

     //if (!_lat.equals(key._lat)) return false; 
     //if (!_lon.equals(key._lon)) return false; 
     return o instanceof Key 
     && (_lat.equals(((Key)o)._lat)) 
     && (_lon.equals(((Key)o)._lon)); 


     //return true; 
    } 

    @Override 
    public int hashCode() { 

     return _lat.hashCode() + _lon.hashCode(); 

    } 
} 

private Vector<ArrayList<Applicationdataset>> putIntoVector(Map<Key, ArrayList<Applicationdataset>> myMap) 
{ 
    Vector<ArrayList<Applicationdataset>> mvectorlist = new Vector<ArrayList<Applicationdataset>>(); 
    Iterator<Entry<Key, ArrayList<Applicationdataset>>> miterator = myMap.entrySet().iterator(); 
    while (miterator.hasNext()) { 
     Map.Entry<Key, ArrayList<Applicationdataset>> pairs = (Map.Entry<Key, ArrayList<Applicationdataset>>)miterator.next(); 
     mvectorlist.add(pairs.getValue()); 
     miterator.remove(); // avoids a ConcurrentModificationException 
    } 

    return mvectorlist; 

} 
} 

任何人幫助我,我無法解決這個問題,直到現在接近大約10個小時,我一直在花錢看到這個..

+0

不要這樣調用條件if(myvectorlist!= null && myvectorlist.size()!= 0) ){}}「 – rajpara

+0

@AndroidCoader:對不起,但這只是錯誤的建議。我在代碼審查中提到了很多關於上述代碼的事情,但括號內有兩個ANDed條件並沒有錯。另外,請在您的評論中格式化代碼。如果應該更改該行的任何內容,則應將第二個條件更改爲'!list.isEmpty()'而不是'list.size()!= 0',並應爲mvectorlist選擇一個更好的名稱。 –

+0

對不起,但這不是例外的原因,請指導我需要謝謝你.. – sandee

回答

3

java.lang.ArrayIndexOutOfBoundsException: index=0 length=0意味着你試圖訪問一個ArrayList當e尺寸爲零。回顧一下你的代碼,確保你沒有試圖訪問一個空的列表。

編輯:

好吧,我想我知道你是什麼問題。我認爲您使用createItem嘗試爲您的ItemizedOverlay添加疊加層。這個函數名字很差,實際上並沒有創建一個項目。它只是返回您的數組OverlayItems中的一個項目。您需要使用GoogleMapViewOverlay.addOverlay(OverlayItem)才能真正將OverlayItem添加到地圖中。嘗試創建一個測試覆蓋與隨機座標,看看是否可行。請記住停止使用createItem()並開始使用addItem()。如果這不是你的問題,那麼發佈你所有的代碼,也許我們可以幫助你更好一點。

+0

對不起先生,請參閱堆棧跟蹤.. – sandee

+0

堆棧跟蹤顯示,您在後臺嘗試繪製其索引不存在的項目。 – crocboy

+0

事實上,我收到了同樣的錯誤,通常是因爲試圖繪製一個'Overlay'對象,或者沒有正確實例化,或者'null'。 – crocboy

0

我不知道在什麼時候在你的代碼中的錯誤引起的,但你可以嘗試改變:

if (size == 1) { 
} else { 
} 

到:

if (size == 1) { 
} else if (size > 1) { 
} 

這可能擺脫你的ArrayIndexOutOfBoundsException異常

+0

對不起,先生,但這不是例外的原因,請指導需要的方式.. – sandee