【发布时间】:2020-07-28 04:09:28
【问题描述】:
我是 Android 编程新手。 我编写了一个程序来在 ESRI mapview (100.7.0) 中显示 mmpk。当 mapview 在 MainActivity 中时,应用程序运行良好。 但是,当单击 MainActivity 中的按钮时,会调用 MapActivity 并导致应用崩溃。
请帮帮我
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button);
}
public void btnClick(View view) {
Intent intent = new Intent(MainActivity.this,MapActivity.class);
startActivity(intent);
}
}
地图活动
public class MapActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static String extSDCardDirName;
private static String filename;
private static String mmpkFilePath;
private MapView mMapView;
private MobileMapPackage mapPackage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the directory
extSDCardDirName =Environment.getExternalStorageDirectory() + File.separator + "mmm/";
// get mobile map package filename
filename ="maps.mmpk";
// create the full path to the mobile map package file
mmpkFilePath = extSDCardDirName + filename;
Log.d("FilePath", mmpkFilePath);
// retrieve the MapView from layout
mMapView = (MapView) findViewById(R.id.map);
loadMobileMapPackage(mmpkFilePath);
}
private void loadMobileMapPackage(String mmpkFile) {
// create the mobile map package
mapPackage = new MobileMapPackage(mmpkFile);
// load the mobile map package asynchronously
mapPackage.loadAsync();
// add done listener which will invoke when mobile map package has loaded
mapPackage.addDoneLoadingListener(new Runnable() {
@Override
public void run() {
// check load status and that the mobile map package has maps
if (mapPackage.getLoadStatus() == LoadStatus.LOADED && !mapPackage.getMaps().isEmpty()) {
// add the map from the mobile map package to the MapView
mMapView.setMap(mapPackage.getMaps().get(0));
mMapView.setAttributionTextVisible(false);
//layer = new GraphicsLayer();
} else {
// log an issue if the mobile map package fails to load
Log.e(TAG, mapPackage.getLoadError().getMessage());
}
}
});
}
}
错误:
04-15 12:00:43.653 7426-7426/com.mytest.mymap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mytest.mymap, PID: 7426
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.esri.arcgisruntime.mapping.view.MapView.setMap(com.esri.arcgisruntime.mapping.ArcGISMap)' on a null object reference
at com.mytest.mymap.MapActivity$1.run(MapActivity.java:77)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
当以下条件为真时,mapPackage 值不为空。
if (mapPackage.getLoadStatus() == LoadStatus.LOADED && !mapPackage.getMaps().isEmpty()) {
// add the map from the mobile map package to the MapView
mMapView.setMap(mapPackage.getMaps().get(0));
mMapView.setAttributionTextVisible(false);
//layer = new GraphicsLayer();
}
【问题讨论】: