flutter
flutter

Reputation: 6776

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

Just came across butterknife recently. I added the line in my gradle(module : app) file: compile 'com.jakewharton:butterknife:7.0.1'

It synched without any errors. I'm able to import 'butterknife.Butterknife' to my class file where the imports usualyy go. But cant import butterknife.InjectView doesn't seem to be there? Any suggestions?

Upvotes: 3

Views: 6847

Answers (3)

viper
viper

Reputation: 1906

@InjectView is no more available and is replaced by @BindView. We will have to import Butterknife dependencies to use the annotations. More on butter knife here :- http://jakewharton.github.io/butterknife/

@BindView annotation could be implemented as :-

@BindView(R.id.button_id)

Note that you will need to call ButterKnife.bind(this); from onCreate() method of the main activity to enable Butterknife annotations. An example on 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);

    }
}

If you are using Butterknife in a fragment then use Butterknife.bind(this,view) the view being the fragment view i.e. :-

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_other_product_category, container, false);
    ButterKnife.bind(this, view);
    return view;
}

Upvotes: 5

Derek
Derek

Reputation: 1572

The Butterknife 7.0.0 release included the breaking change of renaming of the annotation verbs. This is highlighted in the changelog and reflected in 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 up-to-date, introduction to the usage is at http://jakewharton.github.io/butterknife/

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

Upvotes: 5

eddykordo
eddykordo

Reputation: 607

Obviously @InjectView was replaced by @Bind.

Furhtermore you have to call ButterKnife.bind(this); in your onCreate().

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

Upvotes: 3

Related Questions