首页 / 浏览问题 / 移动GIS / 问题详情
MapView 对象为 null
30EXP 2023年08月01日

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.supermap.mapping.MapControl com.supermap.mapping.MapView.getMapControl()' on a null object reference

行业导航示例时出现调用 getMapControl() 方法时,MapView 对象为 null

private boolean openWorkspace() {

    m_Workspace = new Workspace();
    WorkspaceConnectionInfo info = new WorkspaceConnectionInfo();
    info.setServer(dataPath);
    info.setType(WorkspaceType.SMWU);
    boolean isOpen = m_Workspace.open(info);
    if(!isOpen){
        m_MyApp.showInfo("Workspace open failed!");
        return false;
    }
    m_MapView = (MapView) findViewById(R.id.mapview);
    m_MapControl = m_MapView.getMapControl();
    m_Map     = m_MapControl.getMap();
    m_Map.setWorkspace(m_Workspace);


    //加载高德矢量图 作为地图。0为矢量,1为影像
    DatasourceConnectionInfo dsInfo = new DatasourceConnectionInfo();
    dsInfo.setEngineType(EngineType.GaoDeMaps);
    dsInfo.setDriver("WMTS");
    dsInfo.setAlias("gaode");
    Datasource ds = m_Workspace.getDatasources().open(dsInfo);
    m_Map.getLayers().add(ds.getDatasets().get(0),true);

    m_Map.setScale(1/229492.1875);
    m_Map.setCenter(new Point2D(12953693.6950684, 4858067.04711915));
    m_Map.refresh();
    m_MapControl.setGestureDetector(new GestureDetector(longTouchListener));
    return true;
}
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置一些系统需要用到的路径
        requestPermissions() ;
        Environment.setLicensePath(MyApplication.SDCARD + "SuperMap/License");
        Environment.initialization(this);
        m_MyApp = MyApplication.getInstance();
        boolean isOpen = openWorkspace();

        if(isOpen){
            initView();
            initNavigation2();
//       startDefaultNavi();
        }
    }

1个回答

您好,那边检查下"m_MapView = (MapView) findViewById(R.id.mapview)"执行前有没有执行setContentView
3,745EXP 2023年08月01日
加上setContentView方法后又出现无法正确实例化com.supermap.mapping.MapView

 Caused by: android.view.InflateException: Binary XML file line #8 in com.example.supermap_project:layout/activity_sinavi: Binary XML file line #8 in com.example.supermap_project:layout/activity_sinavi: Error inflating class com.supermap.mapping.MapView
     Caused by: android.view.InflateException: Binary XML file line #8 in com.example.supermap_project:layout/activity_sinavi: Error inflating class com.supermap.mapping.MapView
     Caused by: java.lang.reflect.InvocationTargetException
那边初始化后用Environment.getLicenseStatus看下许可是否正常呢
...