@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
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.
As mentioned in the Butterknife docs, If you are using Eclipse, you will need to configure the IDE before the annotations will be processed
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With