Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.