Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 562
Illusion Avatar asked Jan 03 '15 12:01

Illusion


3 Answers

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.

like image 155
jwBurnside Avatar answered Oct 13 '22 11:10

jwBurnside


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

like image 33
Ed George Avatar answered Oct 13 '22 10:10

Ed George


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();
 }
like image 26
Aditya Vyas-Lakhan Avatar answered Oct 13 '22 11:10

Aditya Vyas-Lakhan