首页 / 浏览问题 / 移动GIS / 问题详情
动态图层 清除元素时抛空异常
mxc
112EXP 2018年02月09日

使用产品 SuperMap iMobile 8C(2017) SP1 for Android_811_9429

数据类型 UDB文件型

问题详细描述:

使用DynamicView.clear()方法 清除动态元素,clear()方法内部抛出  java.lang.NullPointerException

log如下:

java.lang.NullPointerException
Attempt to invoke interface method 'boolean java.util.concurrent.Future.cancel(boolean)' on a null object reference

com.supermap.mapping.dyn.DynamicView.clear(DynamicView.java:1867)

我在调用clear()方法前进行了null判断 确定不会null调用的。从日志看是 clear()方法内部抛出的异常

我看了一下clear()的源码 找到抛出的地方

调用代码片段:

望大神解惑 谢谢

1个回答

你好,我这边用8.11版本测试没有重现你的问题,你排查一下自己写的代码,另外用解压包下的samplecode中的DynamicShow范例运行看是否正常。如果还没有找到原因可以贴出更多一点的代码。
2,042EXP 2018年02月09日

我将对象选中和手势监听封装在了一个类中,下面是代码

public class MapGesture {

    public void destroy() {
        if (null!=mControl) {
            this.mControl.removeGeometrySelectedListener(geometrySelectedListener);
        }
    }

    public interface  TapListener{
        void tapSingle(MotionEvent event);
    }

    public interface SelectedListener {
        void onSelected(String layerName, Recordset recordset);
    }

    public interface  DrillListener{
        void drillSingle(Point2D point2D);
    }

    protected MapControl mControl;
    protected DynamicView mDyn;
    protected boolean mTapEnable;
    protected String[] mSelectLayerNames;
    protected SelectedListener mSelectedLis;
    protected TapListener mTapLis;
    protected DrillListener mDrillLis;
    protected DynamicStyle mDynStyle;
    protected boolean mDrillEnable ;    //钻取
    protected Handler mHandler = new Handler();
    public void setmSelectedLis(SelectedListener mSelectedLis) {
        this.mSelectedLis = mSelectedLis;
    }

    public TapListener getmTapLis() {
        return mTapLis;
    }

    public void setmTapLis(TapListener mTapLis) {
        this.mTapLis = mTapLis;
    }

    public void setmDrillLis(DrillListener mDrillLis) {
        this.mDrillLis = mDrillLis;
    }

    public void drill(){
        this.mDrillEnable  = true;
        this.mTapEnable = false;
        if(mControl.getAction()!= Action.SELECT) {
            mControl.setAction(Action.SELECT);
        }
    }

    public void disDrill(){
        this.mDrillEnable = false;
        if (mControl.getAction()!= Action.PAN){
            mControl.setAction(Action.PAN);
        }
    }

    public void tap(){
        this.mTapEnable = true;
        this.mDrillEnable = false;
        mControl.setAction(Action.SELECT);
//        mControl.setSelectionMode(SelectionMode.INTERSECT);

        log("tap", "enable");
    }
    public void disTap(){
        this.mTapEnable =false;
        mControl.setAction(Action.PAN);
        MapGesture.this.mDyn.clear();
        MapGesture.this.mDyn.refresh();
    }

    public void setSelectLayerNames(String[] mSelectLayerNames) {
        this.mSelectLayerNames = mSelectLayerNames;
    }

    public String[] getSelectLayerNames() {
        return mSelectLayerNames;
    }

    public MapGesture(MapView mapView, DynamicView mDyn) {
        this.mControl = mapView.getMapControl();
        this.mDyn = mDyn;
        this.mControl.setGestureDetector(new GestureDetector(CommonUtil.context, simpleOnGestureListener));
        this.mControl.addGeometrySelectedListener(geometrySelectedListener);
    }


    public  void  clear(){
        if (null!=mDyn){
            try {
                mDyn.clear();
                mDyn.refresh();
                log("clear", "clean");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private Runnable clearDyn = new Runnable() {
        @Override
        public void run() {
            clear();
        }
    };
    protected boolean selected;

    GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if (null!=mTapLis){
                mTapLis.tapSingle(e);
            }
            if (!mTapEnable&&!mDrillEnable){
                return super.onSingleTapUp(e);
            }
            if (MapGesture.this.mDyn==null){
                return super.onSingleTapUp(e);
            }
            MapGesture.this.mDyn.clear();
            Point2D point2D = mControl.getMap().pixelToMap(new Point((int) e.getX(),(int) e.getY()));
            if (MapGesture.this.mDynStyle==null){
                MapGesture.this.mDynStyle = new DynamicStyle();
                MapGesture.this.mDynStyle.setBackground(BitmapFactory.decodeResource(mControl.getResources(),R.mipmap.ic_bounds_query));
                MapGesture.this.mDynStyle.setScale(0.5f);
            }
            DynamicPoint point = new DynamicPoint();
            point.setStyle(MapGesture.this.mDynStyle);
            point.addPoint(point2D);
            MapGesture.this.mDyn.addElement(point);
            MapGesture.this.mDyn.refresh();

            if (mDrillEnable&&mDrillLis!=null){
                mDrillLis.drillSingle(point2D);
                return true;
            }
            mHandler.postDelayed(clearDyn,200);

            return true;
        }
    };

    protected GeometrySelectedListener geometrySelectedListener = new GeometrySelectedListener() {
        @Override
        public void geometrySelected(GeometrySelectedEvent geometrySelectedEvent) {
            log("geometrySelected", "click");
            if (!mTapEnable||mSelectLayerNames ==null){
                    clear();
                return;
            }
            Layer layer = geometrySelectedEvent.getLayer();
            if (null==layer){
               clear();
                return;
            }
            double min = layer.getMinVisibleScale();
            double scale = mControl.getMap().getScale();

            if (scale<min){ //当前比例下 该图层不可见 无需查询
                log("geometrySelected", "当前图层不可见");
                clear();
                return;
            }
            //点击的属性 是否包含在可以查询的范围
            int length = mSelectLayerNames.length;
            boolean b = false;
            for (int i = 0; i < length; i++) {
                if (layer.getName().equals(mSelectLayerNames[i])) {
                    b = true;
                    break;
                }
            }
            if (!b) {
                clear();
                return;
            }

            mHandler.removeCallbacks(clearDyn);
            String layerName = layer.getName().split("@")[0];
            Selection selection = layer.getSelection();
            GeoStyle geoStyle = new GeoStyle();
            geoStyle.setLineWidth(2.0);
            geoStyle.setLineColor(new Color(0, 0, 255));
            selection.setStyle(geoStyle);
            Recordset recordset = selection.toRecordset();
            if (null!= mSelectedLis &&recordset.moveFirst()){
                mSelectedLis.onSelected(layerName,recordset);
            }
        }

        @Override
        public void geometryMultiSelected(ArrayList<GeometrySelectedEvent> arrayList) {

        }
    };
}

代码基本就这些了,实在找不出问题
...