ButterKnife onclick is not working

匿名 (未验证) 提交于 2019-12-03 02:06:01

问题:

I injected views perfectly using butterknife library. But when I try to implement listeners, for example onclick I'm not able to implement them. Following java code will help you to understand my problem.

Java code:

public class LoginActivity extends ActionBarActivity{     @InjectView(R.id.toolbar) Toolbar toolbar;     @InjectView(R.id.btn_login) Button login;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.login);         ButterKnife.inject(this);          initialize();         //initListeners();          @OnClick(R.id.btn_login)         public void submit(View view) {           // TODO submit data to server...         }     }      /*private void initListeners() {         @OnClick(R.id.btn_login)         public void login(){          }     }*/      private void initialize() {         setSupportActionBar(toolbar);         getSupportActionBar().setIcon(R.drawable.toolbar_icon);         getSupportActionBar().setTitle(null);         getSupportActionBar().setDisplayShowHomeEnabled(true);     } }

Tell me why it is happening. Anything wrong in code? I've already configured the IDE which is compatible with ButterKnife by using following URL.

http://stackoverflow.com/questions/27754811/onclick-is-not-working-in-implementation-of-butterknife-library

Please give me any suggestions regarding this issue. Thanks in Advance..

回答1:

MainActivity.java

import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.Toast;  import butterknife.ButterKnife; import butterknife.InjectView; import butterknife.OnClick;   public class MainActivity extends ActionBarActivity {       @InjectView(R.id.button)     Button login;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         ButterKnife.inject(this);       }       @OnClick(R.id.button)     void submitButton(View view) {         if (view.getId() == R.id.button) {              Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();         }     } }

and the activity_main.xml part

 

in build.gradle file(app)

dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     compile 'com.android.support:appcompat-v7:22.0.0'     compile 'com.jakewharton:butterknife:6.1.0' }


回答2:

In my case this is my solution

add classpath in gradle(Project)

buildscript {     repositories {         jcenter()     }     dependencies {         classpath 'com.android.tools.build:gradle:2.1.0'         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'     } }

and gradle(Module) add apply and apt

apply plugin: 'com.neenbedankt.android-apt' dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     testCompile 'junit:junit:4.12'        compile 'com.jakewharton:butterknife:8.2.1'     apt 'com.jakewharton:butterknife-compiler:8.2.1' }

file java

@OnClick(R.id.btn_login) public void submit(View view) {   // TODO submit data to server... }


回答3:

make sure you add all required dependencies

dependencies { compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' }


回答4:

You have to move your @OnClick out of the onCreate method, as i did below in the code snippet.

The code i posted below should work as it's supposed to (I also use ButterKnife).

public class LoginActivity extends ActionBarActivity{     @InjectView(R.id.toolbar) Toolbar toolbar;     @InjectView(R.id.btn_login) Button login;      @OnClick(R.id.btn_login)     public void submit(View view) {        // TODO submit data to server...     }      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.login);         ButterKnife.inject(this);          initialize();     }      private void initialize() {         setSupportActionBar(toolbar);         getSupportActionBar().setIcon(R.drawable.toolbar_icon);         getSupportActionBar().setTitle(null);         getSupportActionBar().setDisplayShowHomeEnabled(true);     } }


回答5:

You should've to bind butterKnife before you use the annotations.

Add these dependencies to gradle.build

compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

Then add bind to onCreate ButterKnife.bind(this);

Now do the code to Button. The method should be public and in butterKnife, you won't to add the onClick in XML. And method also should be outside of onCreate It'll automatically get button which you assign using the annotation given at above the method,

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     ButterKnife.bind(this);  } @OnClick(R.id.btn_login) public void submit(View view){      //Do your code here.   }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!