首页 / 浏览问题 / 移动GIS / 问题详情
用iMobile实例代码写的app打不开
30EXP 2023年03月16日
使用产品:iMobile 11i最新产品  操作系统:win11x64

问题详细描述:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileInputStream.close()' on a null object reference

2 个回答

这是主要代码

package com.example.sichuan;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.supermap.data.Environment;
import com.supermap.data.Workspace;
import com.supermap.data.WorkspaceConnectionInfo;
import com.supermap.data.WorkspaceType;
import com.supermap.mapping.MapControl;
import com.supermap.mapping.MapView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MapControl mapControl = null;
        String rootPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();

        //设置许可文件路径(默认路径“/SuperMap/license/”)
        Environment.setLicensePath(rootPath + "/supermap1/license/");

        //组件功能必须在Environment初始化之后才能调用
         Environment.initialization(this);

        setContentView(R.layout.activity_main);

        //打开工作空间
        Workspace m_workspace = new Workspace();
        WorkspaceConnectionInfo info = new WorkspaceConnectionInfo();
        info.setServer(rootPath + "/supermap1/GeometryInfo/SiChuan.smwu");
        info.setType(WorkspaceType.SMWU);
        m_workspace.open(info);

        //将地图显示控件和工作空间关联
        MapView m_mapView = findViewById(R.id.Map_view);
        mapControl = m_mapView.getMapControl();
        mapControl.getMap().setWorkspace(m_workspace);

        //打开工作空间中的第二幅地图
        String mapName = m_workspace.getMaps().get(1);
        mapControl.getMap().open(mapName);
        mapControl.getMap().refresh();
    }
}
30EXP 2023年03月16日
您好,1.请注意build.gradle里面compilesdkversion与targetsdkversion不要超过28,因为android28之后对于app访问做了限制,只能访问app自身的目录,所以获取为null,2.build.gradle里面注意加上ndk
3,660EXP 2023年03月17日
你好,我刚开始设置的时候就是这样,还是不行。

这是build.gradle里面的代码

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.example.sichuan"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters 'arm64-v8a'
        }

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation files('libs\\com.supermap.data_v1101.jar')
    implementation files('libs\\com.supermap.mapping_v1101.jar')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}
添加动态权限后运行正常
...