How do I make the menuButton (RadioButton) selected according to the map type?

0

I have a Fragment that gives error whenever I start it. It generates the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
        at mz.co.bm.bmtrack.MapFragment.onPrepareOptionsMenu(MapFragment.java:175)
        at android.support.v4.app.Fragment.performPrepareOptionsMenu(Fragment.java:2475)
        at android.support.v4.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:3295)
        at android.support.v4.app.FragmentController.dispatchPrepareOptionsMenu(FragmentController.java:331)
        at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:480)
        at android.support.v7.view.WindowCallbackWrapper.onPreparePanel(WindowCallbackWrapper.java:98)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onPreparePanel(AppCompatDelegateImplBase.java:359)
        at android.support.v7.view.WindowCallbackWrapper.onPreparePanel(WindowCallbackWrapper.java:98)
        at android.support.v7.app.ToolbarActionBar$ToolbarCallbackWrapper.onPreparePanel(ToolbarActionBar.java:522)
        at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:456)
        at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:57)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:618)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

In this code:

EvenputtingthesechecksinonCreateOptionMenu,generatesthesameerror.

This error is generated when I click on Location in the BottomNavigationView. The BottomNavigation Listener is as follows:

public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {

                case R.id.btm_route:
                    // TODO

                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.fragment, new RotaFragment())
                            .commit();


                    return true;
                case R.id.btm_localizacao:
                    // TODO

                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.fragment, new MapFragment())
                            .commit();

                    return true;
                case R.id.btm_fuel:
                    // TODO

                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.fragment, new FuelFragment())
                            .commit();

                    return true;
            }
            return false;
        }
    
asked by anonymous 28.05.2018 / 10:13

1 answer

0

I've already figured it out. Just create a method to check if the map is null and configure the menu later.

public boolean isMapNull() {
     if (map != null) {
         return false;
     } else {
         return true;
     }
}

And put it in onPrepareOptionMenu ()

 @Override
 public void onPrepareOptionsMenu(Menu menu) {

     if (!isMapNull()) {

         terreno = menu.findItem(R.id.terreno_maptype);
         satelite = menu.findItem(R.id.satelite_maptype);
         hibrido = menu.findItem(R.id.hibrido_maptype);

         int t = GoogleMap.MAP_TYPE_TERRAIN;
         int s = GoogleMap.MAP_TYPE_SATELLITE;
         int h = GoogleMap.MAP_TYPE_HYBRID;

         if (map.getMapType() == t) {

             alterarTipoParaTerreno();
             terreno.setChecked(true);

         } else if (map.getMapType() == s) {

             alterarTipoParaSatelite();
             satelite.setChecked(true);

         } else if (map.getMapType() == h) {

             alterarTipoParaHibrido();
             hibrido.setChecked(true);
         }
     }

     super.onPrepareOptionsMenu(menu);
 }
    
05.07.2018 / 08:23