Butterknife Fragment Button not working
Asked Answered
B

3

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>
Beliabelial answered 9/6, 2014 at 18:1 Comment(5)
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.Zabaglione
Show your me R.layout.fooSiloam
No, it is not part of a library, nor is my Fragment class subclassed from any class other than Fragment.Beliabelial
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.Beliabelial
what solution you get for this, I am also facing sameMacaroon
P
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

Periodicity answered 2/2, 2015 at 20:35 Comment(0)
S
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;
}
Shalandashale answered 26/2, 2017 at 21:52 Comment(0)
F
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.

Frost answered 2/7, 2014 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.