简体   繁体   中英

OnClick ButterKnife, nothing happens

I'm trying to use ButterKnife to onClick. I did the code bellow and nothing happens, I've watched tutorials all over the internet, and they do the same thing as I did.

Here is the code

@BindView(R.id.startButton) protected ImageButton mStartButton;

@OnClick(R.id.startButton)
public void startTest(){
    Toast.makeText(this, "testing", Toast.LENGTH_LONG).show();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(MainActivity.this);

}

And if I put the method startTest(); inside the OnCreate, the toast is called when the app runs for the first time, what shows that the ButterKnife is working. But I need that to happen only when the button is clicked.

Thanks

You mentioned that you have compile 'com.jakewharton:butterknife:8.4.0' in your build.gradle file. I think you may be missing the corresponding compiler. Add this to your dependencies section:

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

The only two reason i can see are either

  1. 'protected' needs to be removed from your ImageView

or

  1. 'startButton' is not actually defined in activity_main

I think your onCreate method should be public. Try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM