Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Butterknife Fragment Button not working

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>
like image 317
Perry Hoekstra Avatar asked Jun 09 '14 18:06

Perry Hoekstra


2 Answers

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;
}
like image 118
Jonathan Nolasco Barrientos Avatar answered Oct 12 '22 02:10

Jonathan Nolasco Barrientos


For latest ButterKnife 8.0.1 June -2016

InjectView and inject has been replaced with BindView and bind This are the steps to follow:-

down vote unaccept From the Butterknife github page:

Add this to you project-level build.gradle:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

Add this to your module-level build.gradle:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

And then use Butterknife like this

 private Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View calcView = inflater.inflate(R.layout.content_main, container, false);

        unbinder = ButterKnife.bind(this, calcView);

        return calcView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick(R.id.one)
    public void one() {

        Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
        result.setText(result.getText().toString() + 1);
    }
like image 31
Hitesh Sahu Avatar answered Oct 12 '22 02:10

Hitesh Sahu