首页 / 浏览问题 / 移动GIS / 问题详情
Environment.initialization失败报空指针
3EXP 2022年11月09日

使用产品:supermap-imobile-10.2.1-228-android-zip-chs 操作系统:huawei nova10,鸿蒙2,安卓11;
数据类型:文件型
问题详细描述:使用浅谈SuperMap iMobile for Android在Android Studio平台开发应用这个文章中的步骤,集成,进入页面崩溃。
报错信息:

Process: com.bonc.ioc.demo.supermap, PID: 10261 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bonc.ioc.demo.supermap/com.bonc.ioc.demo.supermap.DemoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.close()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2976) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3113) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1858) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6820) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.close()' on a null object reference at com.supermap.data.Environment.writeFile(Environment.java:2320) at com.supermap.data.Environment.checkDevice_New(Environment.java:836) at com.supermap.data.Environment.initialization(Environment.java:626) at com.bonc.ioc.demo.supermap.DemoActivity.onCreate(DemoActivity.java:25) at android.app.Activity.performCreate(Activity.java:7224) at android.app.Activity.performCreate(Activity.java:7213) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2956)

gradle配置: 

plugins {
    id 'com.android.library'
}

android {
    compileSdk 32
    defaultConfig {
        minSdk 23
        targetSdk 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk{
            abiFilters 'armeabi-v7a'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation files('libs\\amqp-client-4.11.3.jar')
    implementation files('libs\\chartengine.jar')
    implementation files('libs\\classes.jar')
    implementation files('libs\\com.github.mikephil.charts.jar')
    implementation files('libs\\com.huawei.hms.arenginesdk3.0.0.11.jar')
    implementation files('libs\\com.supermap.ai_v1021.jar')
    implementation files('libs\\com.supermap.analyst_v1021.jar')
    implementation files('libs\\com.supermap.ar_v1021.jar')
    implementation files('libs\\com.supermap.data_v1021.jar')
    implementation files('libs\\com.supermap.mapping_v1021.jar')
    implementation files('libs\\com.supermap.mdatacollector_v1021.jar')
    implementation files('libs\\com.supermap.messagequeue_v1021.jar')
    implementation files('libs\\com.supermap.navigation_v1021.jar')
    implementation files('libs\\com.supermap.plot_v1021.jar')
    implementation files('libs\\com.supermap.realspace_v1021.jar')
    implementation files('libs\\com.supermap.services_v1021.jar')
    implementation files('libs\\com.supermap.track_v1021.jar')
    implementation files('libs\\gson-2.2.2.jar')
    implementation files('libs\\httpmime-4.1.1.jar')
    implementation files('libs\\Java-WebSocket-1.3.5.jar')
    implementation files('libs\\jaxb-api-2.2.2.jar')
    implementation files('libs\\jsoup-1.10.1.jar')
    implementation files('libs\\lang3-3.1.0.jar')
    implementation files('libs\\log4j-1.2.17.jar')
    implementation files('libs\\netty-all-4.1.36.Final.jar')
    implementation files('libs\\okhttp-3.9.0.jar')
    implementation files('libs\\okio-1.13.0.jar')
    implementation files('libs\\OpenGLTileExtent.jar')
    implementation files('libs\\pinyin4j-2.5.1.jar')
    implementation files('libs\\slf4j-api-1.7.26.jar')
    implementation files('libs\\slf4j-log4j12-1.7.26.jar')
    implementation files('libs\\starcore_android_r3.76.jar')
    implementation files('libs\\tensorflow-mobile.jar')
    implementation files('libs\\xercesImpl-2.12.0.jar')
    implementation files('libs\\XML-json.jar')
}


menifest配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bonc.ioc.demo.supermap">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"/>

    <application>

        <activity
            android:name=".DemoActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MapControl mapControl = null;
    String rootPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
    //设置许可文件路径(默认路径“/SuperMap/license/”)
    Environment.setLicensePath(rootPath + "/SuperMap/license/");
    //组件功能必须在Environment初始化之后才能调用
    Environment.initialization(this);

    setContentView(R.layout.activity_demo);

    //打开工作空间
    Workspace m_workspace = new Workspace();
    WorkspaceConnectionInfo info = new WorkspaceConnectionInfo();
    info.setServer(rootPath + "/SampleData/GeometryInfo/World.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();
}

1个回答

您好,1.这是因为build.gradle中compilesdk与targetsdk版本不一致导致,建议您那边把compilesdk修改为28,与targetsdk版本一致呢

2.app没有获取到读写权限,导致许可读不到,所以initialize这一步会报错,建议您手动给app许可或者使用如下代码自动获取许可

 private void requestPermissions() { //Android6.0以上设备设置动态权限
        if (Build.VERSION.SDK_INT >= 23) {
            // 检查是否拥有权限
            String[] permissions = {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.CHANGE_WIFI_STATE,
            };
            String permission = null;
            int id = 0;
            boolean isBreak = false;
            int checkCallPhonePermission = 0;
            for (int i = 0; i < permissions.length; i++) {
                permission = permissions[i];
                checkCallPhonePermission = checkSelfPermission(permission);
                if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(permissions, id);
                    isBreak = true;
                    break;
                }
            }
        }

    }
3,740EXP 2022年11月09日
...