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

mDeleteButton - set it to the layout button with id 'deleteButton'

I did the following:

public class PhotoFragment extends Fragment {

    public Button mDeleteButton;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_photo, container, false);
       mDeleteButton = (Button)findViewById(R.id.deleteButton);
        return rootView;
    }
}

but I am getting compiler errors.

I'm not too sure what I've done wrong. I have moved the line I added around the file but that's not helped.

Any thoughts on what mistake I've made?

Steve.

I have only added it once - it's in the onCreate method but I have tried it in the other method too.

2 Answers

Paolo Scamardella
Paolo Scamardella
24,828 Points

First of all you need to get rid of the mDeleteButton = (Button)findViewById(R.id.deleteButton); in the onCreate method. Second, you are dealing with fragments and in your onCreateView method you need to do mDeleteButton = (Button) rootView.findViewById(R.id.deleteButton);

I believe rootView returns your fragment xml layout, and in order to access the button in your fragment layout, you need to use rootView(or whatever variable you give it to).

Hope that helps and make sense!

Perfect - many thanks! :-)