Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2010, 09:38 AM
Registered User
 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Ch 4 menus and context menus

In this chapter, the menu and context menu are set up in code. There is mention of setting them up in xml though and I am trying to do that. I can get the menus to work with xml but not if they are to be dynamic. I wrote the xml files as described in the book. The problem seems to come from onPrepareOptionsMenu(). Specifically it stops at:

MenuItem removeItem = menu.findItem(REMOVE_TODO);

What I mean is that I put breakpoints in at every line in that method and that's as far as it got before the program "stopped unexpectedly". It seems that a nullpointer reference exception is thrown. Also I took the mentioned line out completely and it doesn't crash. I think that it's not finding the menu item like it should. I don't know if I'm supposed to assign numbers some how to each menu item in my xml resources or what. If anyone can help I would really appreciate it. I've uploaded my project to mediafire:

http://www.mediafire.com/file/o1tdsy...t_Menu_XML.zip

Thanks.
 
Old January 5th, 2011, 06:23 PM
Registered User
 
Join Date: Dec 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't know if this might help (I haven't downloaded your XML), but I was running into a similar problem using code to create the options menu, where I was getting null returned from menu.findItem(REMOVE_TODO). I figured out that it was an error on my part in writing the code in onCreateOptionsMenu() when creating the remove menu item ... I had copied the previous line that creates the add menu item but in modifying the copy, I neglected to change 'ADD_NEW_TODO' to 'REMOVE_TODO', so I effectively created two menu items with the same ID (doh!). Which is why it couldn't find the right one in onPrepareOptionsMenu().

So perhaps you've done something similar in your XML (which is what I'm about to attempt myself).
 
Old January 5th, 2011, 07:14 PM
Registered User
 
Join Date: Dec 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, disregard what I said in my prior response (assuming you haven't already figured it out) ... I added an XML file for the options menu and used the MenuInflator method and again got null back from menu.findItem(REMOVE_TODO). But by using the Eclipse debugger to view the Menu object passed to the method, I found that the IDs for the menu items created with the XML file were now some seemingly random values instead of 1 and 2. So after a bit of investigation, I changed:

private static final int ADD_NEW_TODO = Menu.FIRST;
private static final int REMOVE_TODO = Menu.FIRST + 1;

to:

private static final int ADD_NEW_TODO = R.id.item01;
private static final int REMOVE_TODO = R.id.item02;

And that seems to work.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 4 menus and context menus keepjumpin BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 0 August 19th, 2010 10:28 PM
Menus in Chrome, Ch 7 p244- Alanrf BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 9 May 17th, 2010 01:37 PM
showing javascript context menus outside frames... jxn Javascript How-To 5 February 3rd, 2006 07:53 PM
sub menus matshediso HTML Code Clinic 2 January 31st, 2005 05:15 AM
Menus Frank Schmuck BOOK: Beginning ASP.NET 1.0 1 June 6th, 2003 01:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.