@OnClick does not work in the implementation of the ButterKnife library

@OnClick does not work in the implementation of the ButterKnife library

When I click on Button nothing happens.

This is my complete 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

+5
source share
6 answers

As mentioned in Butterknife docs, if you use Eclipse, you need to configure the IDE before the annotations are processed

+10
source

For those who are facing this problem in Android Studio, make sure that you include both the necessary dependencies and the apt plugin in your build files (check the Butterknife readme version). I rushed through the documents and turned on only the compilation dependency, due to which the error binding was interrupted.

+11
source

In your activity, try adding ..

  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(); } 
+3
source

Double check all dependencies in your project. Here are the download instructions from the readme file. Set up the build.gradle project at the project level to enable 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 modular build.gradle level and add 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 later, you do not need the "android-apt" plugin and instead of apt you can use annotationProcessor when declaring compiler dependencies.

+3
source

Use ButterKnife.bind(this); in onCreate () for Activity. or onCreateView for the fragment.

 @OnClick(R.id.button_stop_sticky) public void onStopClicked(View v) { Toast.makeText(this, "onStop Clicked", Toast.LENGTH_LONG).show(); } 

And obviously application module> gradle add dependency

 compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' 
+3
source

Using Butter Knife, you can also relate your opinion in the same way ...

  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 information, you can see this link http://jakewharton.imtqy.com/butterknife/

0
source

Source: https://habr.com/ru/post/1210300/


All Articles