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

Where am I supposed to place the assignment for the button?

I get this error when I try to compile:Bummer! Make sure you are setting the button in the correct lifecycle method for a Fragment! ''' import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button;

public class PhotoFragment extends Fragment {

public Button mDeleteButton;

public void onCreate(Bundle savedInstanceState) {
  mDeleteButton = (Button)findViewById(R.id.deleteButton);

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_photo, container, false);
    return rootView;



3 Answers

You need to move your code to the onCreateView() method. This is the method that initializes the fragment view.

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

Thank you!I have to learn more about rootView now

James N
James N
17,864 Points

this helped me out to!!!