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

(Android, Butterknife) Why isnt @Bind working in this?

EditText topTextInput;
    EditText bottomTextInput;
    Button button;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.top_section_fragment, container, false);

        topTextInput = (EditText) view.findViewById(R.id.topTextInput);
        bottomTextInput = (EditText) view.findViewById(R.id.bottomTextInput);
        button = (Button) view.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClicked(v);
            }
        });

        return view;
    }

This seems to work. I was originally trying to use Butterknife to bind the components like so:

//Bind the components to variables
@Bind(R.id.topTextInput) EditText topTextInput;
@Bind(R.id.bottomTextInput) EditText bottomTextInput;
 @Bind(R.id.button) Button button;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.top_section_fragment, container, false);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClicked(v);
            }
        });

        return view;
    }

This doesnt work. I get an android.view.InflateException: Error inflating class fragment.

Whats going on here? How can I use Butterknife properly in this code?

1 Answer

Hi orbyt,

In your onCreateView method you need to call ButterKnife.bind(this, view); It needs to be after you instantiate your view like so:

//Bind the components to variables
@Bind(R.id.topTextInput) EditText topTextInput;
@Bind(R.id.bottomTextInput) EditText bottomTextInput;
 @Bind(R.id.button) Button button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.top_section_fragment, container, false);
        ButterKnife.bind(this, view);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClicked(v);
            }
        });

        return view;
    }

I hope this helps, if not let me know.

thanks!