Menu in MapActivty not working

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Menu in MapActivty not working

Postby himym » Wed Aug 04, 2010 8:38 am

Hey.
I have an Activity which is showing a Map (MapActivity). I am trying to add a OptionsMenu, but it just won't show up. And as I noticed the OnCreateOptionsMenu is not being called at all. Is there anything different on a MapActivity than on a normal Activty in handling the Menu?
Can anybody help, please!!

Thanks
Regards
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Top

Re: Menu in MapActivty not working

Postby blundell » Wed Aug 04, 2010 8:57 am

Nope no difference :-)

Implement an options menu:
Code: Select all
/** hook into menu button for activity */
        @Override public boolean onCreateOptionsMenu(Menu menu) {
          populateMenu(menu);
          return super.onCreateOptionsMenu(menu);
        }

        /** when menu button option selected */
        @Override public boolean onOptionsItemSelected(MenuItem item) {
          return applyMenuChoice(item);
        }
       
        public boolean applyMenuChoice(MenuItem item) {
                switch (item.getItemId()) {
                case 1:
                        showSomething();
                        return true;
                case 2:
                        checkSomething();
                        return true;
                default:
                        checkMapLocation();
                        return true;
                }
        }
       
        public void populateMenu(Menu menu) {

                  MenuItem item1 = menu.add(0, 1, Menu.NONE, "Find Something");
                  {                     
                    item1.setIcon(android.R.drawable.ic_menu_directions);
                  }

                  MenuItem item2 = menu.add(0, 2, Menu.NONE, "Check Something");
                  {
                    item2.setIcon(android.R.drawable.ic_menu_compass);
                  }
            }
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Menu in MapActivty not working

Postby himym » Wed Aug 04, 2010 9:14 am

I do have almost the same, now changed it to your code. But still, it isn't calling the onCreateOptionsMenu method, and so not showing the menu. This one should be called automatically, right?
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Re: Menu in MapActivty not working

Postby blundell » Wed Aug 04, 2010 10:01 am

YEs. You must have some other code blocking it. do you implement onKeyPressed or something?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Menu in MapActivty not working

Postby himym » Wed Aug 04, 2010 10:31 am

ok. using the keylistener or sth similar is a pretty idea. I do have the problem, what would the argument be in the onCretaeOptionsMenu though?

Code: Select all
public boolean onKeyUp(View view, Editable text, int keyCode, KeyEvent event) {
      // TODO Auto-generated method stub
      if(keyCode == KeyEvent.KEYCODE_MENU){
         onCreateOptionsMenu(what to put here);
      }
      return false;


Code: Select all
public boolean OnCreateOptionsMenu(Menu menu){
      populateMenu(menu);
      return super.onCreateOptionsMenu(menu);
}
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Re: Menu in MapActivty not working

Postby blundell » Wed Aug 04, 2010 10:41 am

No I didn't say that. That's a BAD idea.

I'm saying do you have something implemented in onKeyDown or onKeyUp that is hogging the key presses and not returning to it's super class?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: Menu in MapActivty not working

Postby himym » Wed Aug 04, 2010 10:52 am

no. actually, I didnt have any KeyListener before you mentioned it
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Re: Menu in MapActivty not working

Postby blundell » Wed Aug 04, 2010 11:05 am

You could paste your class, let's see what's going on.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Menu in MapActivty not working

Postby himym » Wed Aug 04, 2010 11:27 am

Yea. This would be my Class:
Code: Select all
public class GM1 extends MapActivity{
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
   }
   
   public boolean OnCreateOptionsMenu(Menu menu){
      Log.d("menu", "creating menu");
      populateMenu(menu);
      return super.onCreateOptionsMenu(menu);

   }
   
   public void populateMenu(Menu menu){
      menu.add(0, 1, 1, "Test1");
      menu.add(0, 1, 2, "test2");
   }
   
   public boolean OnOptionsItemSelected(MenuItem item){
      switch(item.getItemId()){
      case 1:
         one();
         return true;
      case 2:
         two();
         return true;
         default:
            return true;
      }
   }
   
   private void one(){
      Log.d("menu", "Test1 pressed");
   }
   
   private void two(){
      Log.d("menu", "test2 pressed");
   }
   
   public MapView googleMaps(double lat, double lng){
      MapView mv = (MapView)findViewById(R.id.mapView);
      mv.setBuiltInZoomControls(true);
      return mv;
   }
   @Override
   protected boolean isRouteDisplayed() {
      // TODO Auto-generated method stub
      return false;
   }

Thank you so much for trying to help me.
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Re: Menu in MapActivty not working

Postby blundell » Wed Aug 04, 2010 1:19 pm

Your method names start with a capital letter and don't have the @override annotation ................

Please refer to my first post
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Menu in MapActivty not working

Postby himym » Fri Aug 06, 2010 10:21 am

I am sorry to come back to this thread just now, but I was busy the last days.
So I tried to change that capitalletter, but still no different behaviour. So no menu is popping up when pressing the menu button :?
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Re: Menu in MapActivty not working

Postby blundell » Fri Aug 06, 2010 10:55 am

blundell wrote:Your method names start with a capital letter and don't have the @override annotation ................

Please refer to my first post
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Menu in MapActivty not working

Postby himym » Fri Aug 06, 2010 11:06 am

ok well. I might have figuered out my problem.
Your suggestion is really working. But the thing is my MapActivity is running in a Tab. So when I have the Map as a solo Activity everything is fine, but when I have it run in a TabLayout I get thetrouble that the menu is not created.
You have any suggestion for that problem?

Thanks a lot
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Re: Menu in MapActivty not working

Postby pranav09 » Mon Aug 09, 2010 6:59 am

It's a Override method. It should be called automatically when you press Menu. Well, try the below code.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         public boolean onCreateOptionsMenu(Menu menu)
  3.         {
  4.                 menu.add(0, 0, 0, "First");
  5.                 menu.add(0, 1, 1, "Second");
  6.                 return super.onCreateOptionsMenu(menu);
  7.         }
  8.        
  9.         @Override
  10.         public boolean onOptionsItemSelected(MenuItem item)
  11.         {
  12.                 switch(item.getItemId())
  13.                 {
  14.                 case 0:
  15.                         Toast.makeText(getApplicationContext(), "First Menu", Toast.LENGTH_SHORT).show();
  16.                         break;
  17.                 case 1:
  18.                         Toast.makeText(getApplicationContext(), "Second Menu", Toast.LENGTH_SHORT).show();
  19.                         break;
  20.                 }
  21.                 return super.onOptionsItemSelected(item);
  22.         }
  23.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


If still it doesn't work for you, eclipse gives functionality to add Override methods. Follow the below path to add Override methods as you need.

Source -> Override/Implement Methods...

Thanks.
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Re: Menu in MapActivty not working

Postby himym » Mon Aug 09, 2010 9:47 am

If still it doesn't work for you, eclipse gives functionality to add Override methods. Follow the below path to add Override methods as you need.

Source -> Override/Implement Methods...


Ok...and the weird thing continues......
There is no onCreateOptionsMenu. For the MaoActivity I only do have: isLocationDisplayed(), onDestroy(), onGetMapDataSource(), onNewIntent(), onPause() and onResume().
For all the other Activities I do have quiet some more Mehtods to override.
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Top
Next

Return to Map Problems

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 7 guests