Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Handling Clicks / Why Android menu do not show next image when touching icon?

For some reason when touching the icon in the menu to show a next image the code works but the image doesn't show it.

Code:

@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.nextImage: imageIndex++; if (imageIndex >= imageResIds.length){ imageIndex = 0; loadImage(); break; } } return super.onOptionsItemSelected(item); }

1 Answer

Your closing bracket for the if block is misplaced.

loadImage();
break;

should be outside the block. In the video, Ben is not using any bracket because the statement only consists of one line. Some personal preference.

The way your code is now, you're only increasing the imageIndex, but never loading the image corresponding to that index.

Also, it is much easier to read your code if you format it. Just add 3 backticks (```) before your code, and after and the magic will happen. Hope that helps.

Thanks for your help, everything works fine now.

You're welcome, glad to hear that :)