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 Build a Self-Destructing Message Android App Using Fragments for Tabs Understanding the Rest of the Pieces

Trouble with the Understanding the rest of the pieces code challenge

I have been trying to figure out where to place

mDeleteButton = (Button)findViewById(R.id.deleteButton);

to make the first part of the code challenge to work, but nothing seems to take it. I'm not sure what I am doing wrong. I believe that this code is correct.

4 Answers

What error are you getting from the challenge engine?

If you are working with fragments, remember that you will have to call findViewById() from another view. It would look something like:

mDeleteButton = (Button) rootView.findViewById(R.id.deleteButton);

Edit: Make sure that you put your code that handles Views in onCreateView(). This is what you have to do for Fragments, unlike Activities.

was rootView mentioned in the vid?

Timothy Boland
Timothy Boland
18,237 Points

I think this code challenge is a little premature, because the video dosen't discuss how to use onCreateView yet

The error that I get when my put my code anywhere but inside of the onCreate method is Bummer! There is a compiler error. Please click on preview to view your syntax errors!

When I put my code inside the onCreate method I get

Bummer! Make sure you are setting the button in the correct lifecycle method for a Fragment!

Edited my original answer.

Thank you for your help! So for fragments I need to set the buttons in the onCreateView()?

Yep! Almost like how in Activities, you have to call setContentView() before you can start calling on UI elements.