How to make the menuItem (Radiobutton) be selected according to the type of map?

Asked

Viewed 23 times

0

I have a Fragment that misses every time 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 that code: inserir a descrição da imagem aqui

Even putting these checks on onCreateOptionMenu, generates the same error.inserir a descrição da imagem aqui

This error is generated when I click on Location in 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;
        }
  • You say it is an error when you start Fragment but then you say it is when you click the button. Which of the two is?

  • It’s when I click the button and, soon gives error! I’m sorry for the confusion!

  • The error indicates that the variable map is void.

  • Yeah! But, she’s been initialized! So much so that when I start the app, it opens normally, the problem is when I click on those buttons! I don’t understand what’s wrong!

  • Add the full code relevant to your problem to the question

  • I added the Bottomnavigationview Listener, I don’t know yet I need to add something else.

Show 1 more comment

1 answer

0


I 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 on 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);
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.