Skip to content Skip to sidebar Skip to footer

@OnClick Is Not Working In Implementation Of ButterKnife Library

@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 ActionBar

Solution 1:

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.


Solution 2:

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


Solution 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();
 }

Solution 4:

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.


Solution 5:

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'

Post a Comment for "@OnClick Is Not Working In Implementation Of ButterKnife Library"