Android Studio: Integrating Butterknife?

梦想与她 提交于 2019-12-03 11:51:35

I think your code is good,

  1. Try to sync your gradle by click

  2. Try to go File -- invalidate Caches and restart your Android studio.

Also, don't forget put ButterKnife.inject(this); in onCreate()

Note that in the latest versions of the ButterKnife library, the @InjectView() annotation is no longer used.

In stead @Bind(R.id.tvHelloWorld) and ButterKnife.bind(this); are used.

Reference: http://jakewharton.github.io/butterknife/

So ButterKnife just got updated with version 8.5.1

To use that,

Add below line inside project-level build.gradle:

classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'

Add below lines inside app-level build.gradle:

// Field and method binding for Android views
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

Apply butterknife plugin as below:

apply plugin: 'com.jakewharton.butterknife'

A little change in syntax to previous ButterKnife version is that, now you have to use R2 instead of R while using ButterKnfe annotations.

To be specific:

Instead of writing

@BindView(R.id.textView)
TextView mTextView;

We will be writing

@BindView(R2.id.textView)
TextView mTextView;

and then simply build the project.

Set up manual configuration for ButterKnife from this link

File -> Other Settings -> Default Settings

Compiler -> Annotation Processors -> Check Enable annotation processing

Move the TextView tv1 declaration inside your class. Also call the ButterKnife.inject(this); method.

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends ActionBarActivity {

    @InjectView(R.id.tvHelloWorld) TextView tv1;

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

}

I tried all the answer's but nothing is working for me because in the library they changed something. Insteded of @InjectView try @Bind. It will work fine

In the latest versions of the ButterKnife library(8.5.1), the @InjectView() annotation is not used.

You can use @BindView(Component) instead of @InjectView(Component) and instead of using Butterknife.inject(this) use ButterKnife.bind(this)

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