@OnClick is not working in implementation of ButterKnife Library
Asked Answered
T

6

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

Thingumajig answered 3/1, 2015 at 12:23 Comment(7)
did you call ButterKnife.inject(this, view); or ButterKnife.inject(this);?Lapidary
ButterKnife.inject(this);Thingumajig
@shayanpourvatan have to change ButterKnife.inject(this) to ButterKnife.inject(this, view)??Thingumajig
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 tooLapidary
I am using Activity, there is any requirement to enable annotations ?Thingumajig
post xml file and onCreate method pleaseLapidary
@shayanpourvatan this is my code and just added butterknife-6.0.0 in projectThingumajig
E
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

Elenor answered 3/1, 2015 at 12:49 Comment(0)
R
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.

Rickierickman answered 3/6, 2016 at 16:55 Comment(1)
you can check out this link for more details: sitepoint.com/tidying-code-with-android-butterknifeLandwaiter
E
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

Elenor answered 3/1, 2015 at 12:49 Comment(0)
A
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();
 }
Antagonism answered 3/1, 2015 at 12:34 Comment(1)
.inject has been changed .bind in the latest librariesBaeyer
Z
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.

Zuniga answered 14/7, 2016 at 21:34 Comment(0)
G
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'
Grassquit answered 8/11, 2017 at 9:37 Comment(0)
A
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/

Antitype answered 5/1, 2016 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.