使用产品:iMobile 11i 操作系统:win11 x64
问题详细描述:app出现闪退,一下为错误代码:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.supermapimobile2d, PID: 26511
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.supermapimobile2d/com.example.supermapimobile2d.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3754)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3912)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2319)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:257)
at android.app.ActivityThread.main(ActivityThread.java:8228)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1032)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at com.supermap.data.Environment.checkAndLoadLibrary(Environment.java:1612)
at com.supermap.data.Environment.initialization(Environment.java:585)
at com.example.supermapimobile2d.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:8129)
at android.app.Activity.performCreate(Activity.java:8113)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1359)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3727)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3912)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2319)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:257)
at android.app.ActivityThread.main(ActivityThread.java:8228)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1032)
下为代码:
package com.example.supermapimobile2d;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
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 {
private Context context;
@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(context);
setContentView(R.layout.activity_main);
//打开工作空间
Workspace m_workspace = new Workspace();
WorkspaceConnectionInfo info = new WorkspaceConnectionInfo();
info.setServer(rootPath + "/supermap1/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();
}
}