13

I have a Fragment that I am using Butterknife with.

public class FooFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.foo, container, false);

        ButterKnife.inject(this, view);

        return view;
    }

    @OnClick(R.id.some_btn)
    public void someButtonOnClick() {
        // do something
    }
}

However, the someButtonOnClick method is never called when the button is tapped on the screen.

Thoughts on what I am doing wrong with the Butterknife framework?

Here is the layout that is being used:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/White">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/some_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="@dimen/xsmall_margin"
            android:background="@drawable/some_btn_selection" />

        <Button
            android:id="@+id/some_other_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="@dimen/xsmall_margin"
            android:background="@drawable/some_other_btn_selection" />
    </LinearLayout>
</LinearLayout>
Perry Hoekstra
  • 2,687
  • 3
  • 33
  • 52
  • 1
    Probably unrelated (since it would result in a compile error), but any chance you're trying to do this in a library project (vs a normal android app project)? ButterKnife flat-out won't work inside library projects because their R.id's are not final. 2nd Question, are you subclassing this fragment and expecting this to work? I have a feeling that ButterKnife annotations (like OttoBus) are only valid on the actual class being instantiated. If you are subclassing this fragment, then try re-implementing the someButtonOnClick method (with the annotation) in the subclass to confirm the issue. – Geoff Jun 09 '14 at 18:09
  • Show your me R.layout.foo – Than Jun 09 '14 at 18:18
  • No, it is not part of a library, nor is my Fragment class subclassed from any class other than Fragment. – Perry Hoekstra Jun 09 '14 at 23:03
  • I also tried placing a ButterKnife.inject(this) within the onCreate of the Activity, thinking that ButterKnife needed it for the containing activity but alas, there was no joy. I still cannot get it to work for Fragments, just Activity. – Perry Hoekstra Jun 11 '14 at 03:21
  • what solution you get for this, I am also facing same – Amarjit Dec 03 '15 at 07:14

3 Answers3

4

Just use this structure:

@OnClick(R.id.some_btn)
public void someButtonOnClick(View view) {
    // do something
}

This is described here http://developer.android.com/reference/android/R.attr.html#onClick

user3579059
  • 285
  • 1
  • 5
  • 11
3

This works for me:

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(getActivity(), view);
    return view;
}
0

If you are still having issue. Clean the project and try to run. It should fix. If you still have issue, call someButtonOnClick() in onCreate and run and remove it and again run it. It should work. It worked for me.

JJD
  • 50,076
  • 60
  • 203
  • 339
dhiku
  • 1,818
  • 2
  • 22
  • 38