2010-11-15 71 views
4

这并不是指代码中的任何地方。我该如何去了解它的底部?Android Maps NullPointerException ItemizedOverlay

java.lang.NullPointerException 
at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42) 
at com.google.android.maps.MapView.onDraw(MapView.java:494) 
at android.view.View.draw(View.java:6739) 
at android.view.ViewGroup.drawChild(ViewGroup.java:1648) 
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
at android.view.View.draw(View.java:6742) 
at android.widget.FrameLayout.draw(FrameLayout.java:352) 
at android.view.ViewGroup.drawChild(ViewGroup.java:1648) 
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
at android.view.View.draw(View.java:6742) 
at android.widget.FrameLayout.draw(FrameLayout.java:352) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872) 
at android.view.ViewRoot.draw(ViewRoot.java:1422) 
at android.view.ViewRoot.performTraversals(ViewRoot.java:1167) 
at android.view.ViewRoot.handleMessage(ViewRoot.java:1744) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:144) 
at android.app.ActivityThread.main(ActivityThread.java:4937) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
at dalvik.system.NativeStart.main(Native Method) 

这里是ItemizedOverlay

public void draw(Canvas canvas, MapView mapView, boolean shadow) { 

      try { 
      super.draw(canvas, mapView, false); 

      if (getMainOverlayArray().size() != 0){ 

      pfOverlayItem tempOver = null; 
      for (int i = 0; i < getMainOverlayArray().size(); i++) { 

       tempOver = getMainOverlayArray().get(i); 

      boolean isMatch = false; 

      if (tempOver.getTitle().equals(selectedName)) 
      { 
       isMatch = true; 
      } 

      if (isMatch){ 

      Projection projection = mapView.getProjection(); 
      Point point = new Point(); 
      projection.toPixels(tempOver.getPoint(), point); 

      Paint background = new Paint(); 
      background.setColor(Color.WHITE); 
      background.setAlpha(150); 
      RectF rect = new RectF(); 
      rect.set(point.x - 55, point.y +15, 
        point.x + 100, point.y + 65); 
      canvas.drawRoundRect(rect, 5, 5, background); 


      Paint text = new Paint(); 
      text.setAntiAlias(true); 
      text.setColor(Color.BLUE); 
      text.setTextSize(14); 
      text.setTypeface(Typeface.MONOSPACE); 

      canvas.drawText(tempOver.getTitle() , point.x -50 , point.y + 30, text); 
      canvas.drawText(tempOver.getTypeTextOut(), point.x -50 , point.y + 45, text); 
      canvas.drawText(tempOver.getdestination(), point.x -50 , point.y + 60, text); 
      } 
      } 
      } 
      }catch (Exception e){ 
       Log.e("Error", "Problem drawing view", e); 
       e.printStackTrace(); 
      } 
     } 

的绘制方法我现在打印出堆栈跟踪并没有什么区别! :-(

没有人有任何的想法,可以帮助到这条底线

编辑 -

private class SitesOverlay extends ItemizedOverlay<pfOverlayItem> { 
     private List<pfOverlayItem> items = new ArrayList<pfOverlayItem>(); 

     private PopupPanel panel=new PopupPanel(R.layout.popup); 
     private MapLocation selectedMapLocation; 
     private static final int CIRCLERADIUS = 2; 
     private ArrayList<pfOverlayItem> mOverlays = new ArrayList<pfOverlayItem>(); 



     public SitesOverlay() { 

      super(null); 
      //super(boundCenter(defaultMarker)); 
      //items = getMainOverlayArray(); 
      //items = mOverlays; 
      populate(); 

     } 



public void finishedLoading(){ 

    populate(); 

} 

     @Override 
     protected pfOverlayItem createItem(int i) { 
      // Log.i("CREATE","MARKER"); 
      return (items.get(i)); 

     } 

     public void addItem(OverlayItem overlay) { 
      overlay.setMarker(boundCenter(overlay.getMarker(0))); 
      items.add((pfOverlayItem) overlay); 
      //populate(); 
     } 

     public void clearItems(){ 
      runOnUiThread(new Runnable() { 
       public void run() { 
        items.clear(); 
        myMapView.invalidate(); 
       } 
      }); 


     } 

     public void clear() { 
      mOverlays.clear(); 
      myMapView.removeAllViews(); 
      //numItems = 0; 
      // Workaround for another bug with this class: 
      // http://groups.google.com/group/android-developers/browse_thread/thread/38b11314e34714c3 
      setLastFocusedIndex(-1); 
      populate(); 
     } 

     @Override 
     public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
      super.draw(canvas, mapView, false); 

      try { 


      if (getMainOverlayArray().size() != 0){ 

      pfOverlayItem tempOver = null; 
      for (int i = 0; i < getMainOverlayArray().size(); i++) { 

       tempOver = getMainOverlayArray().get(i); 

      boolean isMatch = false; 

      //Log.i("Selected Name",selectedName); 

      if (tempOver.getTitle().equals(selectedName)) 
      { 
       isMatch = true; 
      } 

      if (isMatch){ 

      Projection projection = mapView.getProjection(); 
      Point point = new Point(); 
      projection.toPixels(tempOver.getPoint(), point); 

      Paint background = new Paint(); 
      background.setColor(Color.WHITE); 
      background.setAlpha(150); 
      RectF rect = new RectF(); 
      rect.set(point.x - 50, point.y +15, 
        point.x + 90, point.y + 50); 
      canvas.drawRoundRect(rect, 5, 5, background); 


      Paint text = new Paint(); 
      text.setAntiAlias(true); 
      text.setColor(Color.BLUE); 
      text.setTextSize(14); 
      text.setTypeface(Typeface.MONOSPACE); 

      //canvas.drawRoundRect(rect, 2, 2, background); 
      canvas.drawText(tempOver.getTitle() + " " + tempOver.getcallsign(), point.x -50 , point.y + 30, text); 
      canvas.drawText(tempOver.getdestination() + " " + tempOver.getdraft(), point.x -50 , point.y + 45, text); 
      } 
      } 
      } 
      }catch (Exception e){ 
       Log.e("Error", "Problem drawing view", e); 
       e.printStackTrace(); 

      } 
     } 

     @Override 
     protected boolean onTap(int i) { 

      pfOverlayItem item = getItem(i); 

      if (selectedName.equals(item.getTitle())){ 

       //Toast.makeText(PlaneFinderMain.this, "SECOND MOFO PRESS", 3000).show(); 
       try{  
       Intent myIntent = new Intent(PlaneFinderMain.this, DetailActivity.class); 
       myIntent.putExtra("int", i); 
       myIntent.putExtra("string", selectedName); 
       PlaneFinderMain.this.startActivity(myIntent); 
       }catch (Exception e){ 
        Log.e("Error", "Cannot launch", e); 
       } 
      } 

      currentadshex = item.getmmsi(); 
      new GetRouteTask(item.getmmsi()).execute(); 

      selectedItem = i; 
      selectedName = item.getTitle(); 
      selectedPlanePoint = item.getPoint(); 

      GeoPoint geo=item.getPoint(); 
      Point pt=myMapView.getProjection().toPixels(geo, null); 

      View view=panel.getView(); 

      ((TextView)view.findViewById(R.id.reg)).setText(item.getTitle()); 
      ((TextView)view.findViewById(R.id.flightno)).setText(item.getcallsign()); 
      ((TextView)view.findViewById(R.id.route)).setText(item.getdestination()); 
      ((TextView)view.findViewById(R.id.height)).setText(item.getdraft()); 

      return (true); 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event, MapView mapView) { 

      if (event.getAction() == MotionEvent.ACTION_DOWN){ 

       if (selectedPlanePoint != null){ 
        Projection projection = mapView.getProjection(); 
        Point point = new Point(); 
        projection.toPixels(selectedPlanePoint, point); 

        Point pointHit = new Point(); 
        pointHit.x=(int)event.getX(); 
        pointHit.y=(int)event.getY(); 

        if ((point.x - pointHit.x) >-100 && (point.x - pointHit.x) <70 && (point.y - pointHit.y) < -25 && (point.y - pointHit.y) > -95){ 

          try{  
           Intent myIntent = new Intent(PlaneFinderMain.this, DetailActivity.class); 

            myIntent.putExtra("int", selectedItem); 
            myIntent.putExtra("string", selectedName); 
            PlaneFinderMain.this.startActivity(myIntent); 

           }catch (Exception e){ 
            Log.e("Error", "Cannot launch", e); 
           } 

        }else{ 

        } 


       } 


      } 



      return false; 


     } 



     @Override 
     public int size() { 
      return (items.size()); 
     } 

     public void addOverlay(OverlayItem o){ 
      setLastFocusedIndex(-1); 
      populate(); 
     } 



    } 
+3

看起来您正在将null传递给API参数,特别是在mapview中的某处可能会出现null覆盖。我们应该怎么知道? – Falmarri 2010-11-15 17:22:33

+0

看看你对OverlayBundle.draw()的调用 - 这是我们能做的最好的(更详细的堆栈跟踪会有所帮助) – KevinDTimm 2010-11-15 17:29:43

+0

没有更详细的堆栈跟踪会有所帮助,但有些代码可以找出什么是null和为什么。 – 2010-11-15 17:55:57

回答

5

其实问题并不在于你的ItemizedOverlay的实现,而是在你的MapActivity的实现中。正如你可以从第42行的类似栈跟踪here看到的,OverlayBundle正在调用Overlay的draw()方法。这指向您添加到您的mapView某处的空覆盖。

一些有用的提示: 你绝对应该被拉伸通过取消注释的第一行代码传递给你的超级构造函数(命中测试的使用)你已经在那里:

 public SitesOverlay() { 

     //super(null); 
     super(boundCenter(defaultMarker)); 
     //items = getMainOverlayArray(); 
     //items = mOverlays; 
     populate(); 

    } 

,并可能不叫在draw()方法中使用super.draw()。

@Override 
    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
     // why do you do this if you paint it yourself later on. 
     super.draw(canvas, mapView, false 

最有可能你将不得不实际做两个。

编辑:来想一想,你应该传递一个drawable到你的超级构造函数,因为它用于命中测试。修改了上面的文字以反映这一点。

+0

我尝试在测试应用中为默认标记传递'null';它会产生不同的错误。我也尝试返回一个空覆盖;再次,不同的错误。我无法产生他的堆栈跟踪。但是,还是很好的建议。另外,他不是自己绘制叠加层。他只是在每个叠加层旁边绘制文本标签。但是,他不应该将'false'传递给'super.draw'。 Lee,如果你不想在你的标记上留下阴影,当shadow == false时,你不应该调用super。 – 2010-12-01 13:25:46

+0

谢谢你们,我认为这是非常有用的东西,我会实施,但只有时间才会告诉你! – 2010-12-01 14:24:02

1
pfOverlayItem tempOver = null; 
      for (int i = 0; i < getMainOverlayArray().size(); i++) { 

       tempOver = getMainOverlayArray().get(i); 
boolean isMatch = false; 

      if (tempOver.getTitle().equals(selectedName)) 
      { 
       isMatch = true; 
      } 

是tempOver不是最新的代码28/11满级?分配?

canvas.drawText(tempOver.getTitle() , point.x -50 , point.y + 30, text); 
      canvas.drawText(tempOver.getTypeTextOut(), point.x -50 , point.y + 45, text); 
      canvas.drawText(tempOver.getdestination(), point.x -50 , point.y + 60, text); 

是tempOver.getTitle(),.getTypeTex tOut()或.getdestination()是否为空?

+0

当然,尝试会抓住这个虽然? – 2010-11-15 22:40:35

+0

嗯。这就是问题所在。通过尝试/除了一切,你掩盖了问题。不要这样做。尽可能缩小您的试用范围。你甚至不在你的打印堆栈跟踪。 – Falmarri 2010-11-16 02:19:05

+0

我将错误记录下来。如果我注销e.printStackTrace();是否会向我提供我需要回到Google控制台的信息以帮助追踪此问题? – 2010-11-16 05:18:52

相关问题