@InjectView annotation not working after importing Butterknife in Android Studio?

Stumbled upon butterknife recently. I added a line to my gradle file (module: app): compile 'com.jakewharton: butterknife: 7.0.1'

It syncs without any error. I can import "butterknife.Butterknife" into my class file, which is where the import usually goes. But cant import butterknife.InjectView doesn't seem to exist? Any suggestions?

+3


source to share


3 answers


In the release of Butterknife 7.0.0, an annotation verb renaming violation was introduced. This is highlighted in the changelog and reflected on the website.

Version 7.0.0 *(2015-06-27)*
----------------------------

 * `@Bind` replaces `@InjectView` and `@InjectViews`.
 * `ButterKnife.bind` and `ButterKnife.unbind` replaces `ButterKnife.inject` 
    and `ButterKnife.reset`, respectively.
...

      

https://github.com/JakeWharton/butterknife/blob/f65dc849d80f6761d1b4a475626c568b2de883d9/CHANGELOG.md



A very good and modern introduction to usage - http://jakewharton.github.io/butterknife/

Here's the simplest usage:

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...
  }
}

      

+5


source


Obviously it @InjectView

was replaced by @Bind

.

Furhtermore you should call ButterKnife.bind(this);

in onCreate()

.



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

+3


source


@InjectView

is no longer available and is being replaced by @BindView

. We'll have to import dependencies Butterknife

to use annotations. More details on the oil knife here: - http://jakewharton.github.io/butterknife/

@BindView

annotation can be implemented like: -

@BindView(R.id.button_id)

      

Note that you will need to call the method ButterKnife.bind(this);

from the onCreate()

main activity in order to enable Butterknife annotations. An example of this implementation could be something like: -

public class MainActivity extends AppCompatibilityActivity{
    @BindView(R.id.editText_main_firstName)
    EditText firstName;
    @BindView(R.id.editText_main_lastName)
    EditText lastName;


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

        // Needs to be called to enable Butterknife annotations
        ButterKnife.bind(this);

    }
}

      

+3


source







All Articles