Android 地图 NullPointerException ItemizedOverlay [英] Android Maps NullPointerException ItemizedOverlay

查看:15
本文介绍了Android 地图 NullPointerException ItemizedOverlay的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这根本不涉及我代码中的任何地方.我该如何深入了解它?

This does not refer to anywhere in my code at all. How do I go about getting to the bottom of it?

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的draw方法

Here is the draw method of the 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();
            }
        }

我现在打印出堆栈跟踪,没有区别!:-(

I now print out the stacktrace and no difference! :-(

有没有人有任何想法可以帮助深入了解这个问题?

Does anyone have any ideas that could help get to the bottom of this?

编辑 - 最新代码 28/11 全班

    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();
        }



    }

推荐答案

其实问题不在你的ItemizedOverlay的实现上,而在你的MapActivity的实现上.从类似的堆栈跟踪此处中可以看出,在第 42 行,OverlayBundle 正在调用 draw() 方法的叠加.这指向您添加到 mapView 某处的空 Overlay.

Actually the problem is not in the implementation of your ItemizedOverlay, but in the implementation of your MapActivity. As you can see from a similar stack trace here at line 42 the OverlayBundle is calling the draw() method of an Overlay. This points to a null Overlay that you added to your mapView somewhere.

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

Some helpful hints: You should definitely pass a drawable to your super constructor (used of hit-testing) by uncommenting the first line of code you already have in there:

     public SitesOverlay() {

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

    }

并且可能不会在 draw() 方法中调用 super.draw().

and potentially not call super.draw() in your draw() method.

    @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

很可能您实际上必须两者都做.

Most likely you will have to actually do both.

想想看,您绝对应该将 drawable 传递给您的超级构造函数,因为它用于命中测试.修改了上面的文字以反映这一点.

Come to think of it you should definitely pass a drawable to your super constructor as it is used for hit-testing. Amended the text above to reflect that.

这篇关于Android 地图 NullPointerException ItemizedOverlay的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆