user6866656
user6866656

Reputation:

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

Upvotes: 2

Views: 169

Answers (3)

Michael Dodd
Michael Dodd

Reputation: 10270

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'

Upvotes: 3

Broak
Broak

Reputation: 4187

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

Upvotes: 0

Urma
Urma

Reputation: 320

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

Upvotes: 0

Related Questions