Я новичок в Android программировании. Я написал программу для отображения mmpk в ESRI mapview (100.7.0). Когда просмотр карты находится в 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);
}
}
MapActivity
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());
}
}
});
}
}
Erorr:
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();
}