Show mmpk in MapActivity (ESRI)

459
1
04-15-2020 02:00 AM
mrezagh
New Contributor
Hi
I am new to Android programming. I wrote a program to show mmpk in ESRI mapview (100.7.0) . When the mapview is in MainActivity, the app runs well. but, When clicking the button in the MainActivity, MapActivity is called and app crashs.

Please help me

 

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);
            } else {
                // log an issue if the mobile map package fails to load
                Log.e(TAG, mapPackage.getLoadError().getMessage());
            }
        }
       });
     }
}

Error is:

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)

 

When the following condition is true, the mapPackage value is not null

   

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);
}
0 Kudos
1 Reply
mrezagh
New Contributor

My mistake is copy code from mainactivity to mapactivity !!!                                                                                            

 

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

replace with

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

                                                                                                                                                                                       

 

                                                                                                                                                                                          

                                                                                                                                                                                          

                                                                                                                                                                                          

                                                                                                                                                                                          

                                                                                                                                                                                          

                                                                                                                                                                                          

                                                                                                                                                                                          

                                                                                                                                                                                          

                                                                                                                                                                                        

0 Kudos