首页 / 浏览问题 / 移动GIS / 问题详情
java.lang.NullPointerException
23EXP 2023年08月24日

项目运行过程中发生以下报错:

Process: com.example.onemapview2, PID: 8492
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.onemapview2/com.example.onemapview2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.supermap.data.Datasets com.supermap.data.Datasource.getDatasets()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3771)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3938)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2318)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:210)
at android.os.Looper.loop(Looper.java:299)
at android.app.ActivityThread.main(ActivityThread.java:8306)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1073)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.supermap.data.Datasets com.supermap.data.Datasource.getDatasets()' on a null object reference
at com.example.onemapview2.MainActivity.onCreate(MainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:8194)
at android.app.Activity.performCreate(Activity.java:8160)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3744)

是否因为datasource初始化错误,如何解决呢
书写的代码如下:

        mWorkspace.open(info);
        mMap = new Map(mWorkspace);

        // 添加矢量底图图层
        DatasourceConnectionInfo info1 = new DatasourceConnectionInfo();
        String vectorLayerURL = "http:";  //省略具体写法,是没有问题的
        info1.setAlias("vector");
        info1.setDriver("WMTS");
        info1.setEngineType(EngineType.OGC);
        info1.setServer(vectorLayerURL);
        Datasource datasource1 = mWorkspace.getDatasources().open(info1);
        Dataset dataset1 = datasource1.getDatasets().get(0);
        Layer vectorLayer = mMap.getLayers().add(dataset1,true);

        // 添加地形底图图层(与添加矢量底图图层相似)
        
        // 设置图层名称
        vectorLayer.setCaption("矢量底图");
        terrainLayer.setCaption("地形底图");

        // 保存地图
        boolean isSaved = false;
        try {
            isSaved = mMap.save("mymap");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 显示地图
        if (isSaved) {
            mMap.open("mymap");
        }
    }

(因为评论里回复较慢,所以重新发起提问,麻烦了) 

1个回答

您好,请问下“Datasource datasource1 = mWorkspace.getDatasources().open(info1);”这句执行之后datasource1是否为空呢,如果为空,可能的原因有url不正确、手机没有网络
3,750EXP 2023年08月24日
在其他项目中也使用过这个语句,是能够正常执行的,url是直接复制过来的,用的真机测试,也是有网络的
已解决,谢谢
检查许可是否可用,如许可过期、路径不正确、识别出错原因导致打开web数据源异常
...