5

@OnClick is not working in implementation of ButterKnife Library

When I click on the Button, nothing is happening.

This is my full code:

public class MainActivity extends ActionBarActivity {
    @InjectView(R.id.edit_user)
    EditText username;
    @InjectView(R.id.edit_pass)
    EditText password;

    @OnClick(R.id.btn)
    void submit() {
        // TODO call server...
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        // TODO Use "injected" views...
    }
}

This is my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/edit_user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="user" />

<EditText
    android:id="@+id/edit_pass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="user" />

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

Thanks

7
  • 4
    did you call ButterKnife.inject(this, view); or ButterKnife.inject(this);? Jan 3, 2015 at 12:25
  • 1
    ButterKnife.inject(this);
    – Illusion
    Jan 3, 2015 at 12:26
  • @shayanpourvatan have to change ButterKnife.inject(this) to ButterKnife.inject(this, view)??
    – Illusion
    Jan 3, 2015 at 12:28
  • 2
    you call this from activity or fragment?, if you call from activity this is enough but if you are in fragment or adapter you must pass your view too Jan 3, 2015 at 12:31
  • I am using Activity, there is any requirement to enable annotations ?
    – Illusion
    Jan 3, 2015 at 12:32

6 Answers 6

11

For anyone running into this issue in Android Studio, make sure you are including both of the necessary dependencies and the apt plugin in your respective build files (check the Butterknife readme). I rushed through the docs and only included the compile dependency, which caused binding to fail silently.

1
10

As mentioned in the Butterknife docs, If you are using Eclipse, you will need to configure the IDE before the annotations will be processed

0
3

In your activity try to add..

 ButterKnife.inject(this);

check this code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}

@OnClick(R.id.buttonAlert)
public void alertClicked(View v){
new AlertDialog.Builder(v.getContext())
    .setMessage(getFormattedMessge())
    .setNeutralButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        }
    })
    .show();
 }
1
  • 13
    .inject has been changed .bind in the latest libraries
    – deepwinter
    Jul 15, 2015 at 20:39
3

Double check all dependencies in your project. Here is download instructions from readme file. Configure your project-level build.gradle to include the 'android-apt' plugin:

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

Then, apply the 'android-apt' plugin in your module-level build.gradle and add the Butter Knife dependencies:

apply plugin: 'android-apt'

android {
  ...
}

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

Note: If you are using the new Jack compiler with version 2.2.0 or newer you do not need the 'android-apt' plugin and can instead replace apt with annotationProcessor when declaring the compiler dependency.

3

Use ButterKnife.bind(this); in onCreate() of an Activity. or onCreateView for Fragment.

@OnClick(R.id.button_stop_sticky)
    public void onStopClicked(View v) {

        Toast.makeText(this, "onStop Clicked", Toast.LENGTH_LONG).show();

    }

And obviously, app module > gradle add dependency

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
0

Using Butter Knife you can bind your view like this also...

 class ExampleActivity extends Activity {

    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.subtitle)
    TextView subtitle;
    @Bind(R.id.footer)
    TextView footer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_activity);
        ButterKnife.bind(this);
        // TODO Use fields...
    }
}

for more details you can see this link http://jakewharton.github.io/butterknife/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.