Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Butterknife - binding in fragment

I'm using Butterknife for the first time but something must be wrong. I have a fragment and a Listview and a TextView just for testing but Butterknife wont bind my variables:

public class MyFragment extends Fragment {      @Bind(R.id.resultListView) ListView resultList;      @Bind(R.id.textView1) TextView test;      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         View view = inflater.inflate(R.layout.fragment_my, container, false);         ButterKnife.bind(this, view);         System.out.println(resultList); //null         System.out.println(view.findViewById(R.id.resultListView)); //works         System.out.println(test); //null         System.out.println(view.findViewById(R.id.textView1)); //works         return view;     }  } 

No exception or anything. Manual binding works so my Views must be there.

like image 960
breakline Avatar asked Dec 11 '15 17:12

breakline


People also ask

How do you bind ButterKnife in fragment?

layout. fragment_home, container, false); ButterKnife. bind(this, view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super. onActivityCreated(savedInstanceState); text_input.

Is ButterKnife deprecated?

We decided to update our Android Getting Started Guide Sample App because ButterKnife has been deprecated. As new developers onboard to the Android Communications SDK, it's important for our sample apps to be using the latest Android practices that others would use in their own projects.

What is ButterKnife bind?

Butterknife is a light weight library to inject views into Android components. It uses annotation processing. The @BindView annotation allow to inject views and performs the cast to the correct type for you. The @@OnClick(R.

How do you use ButterKnife in kotlin Android?

Implement Click Listeners for Buttons The first is using setOnClickListener and a lambda function. Then, we will use ButterKnife's @OnClick annotation. Add the following code inside the onCreate() method. Place the code on a new line just below ButterKnife.


1 Answers

This work for me:

Gradle

compile 'com.jakewharton:butterknife:8.6.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' 

Code

. ...  @BindView(R.id.text_input) TextView text_input;  @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     View view = inflater.inflate(R.layout.fragment_home, container, false);     ButterKnife.bind(this, view);     return view; }  @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) {     super.onActivityCreated(savedInstanceState);      text_input.setText("Lorem Ipsum"); ... . 
like image 125
Jonathan Nolasco Barrientos Avatar answered Sep 21 '22 07:09

Jonathan Nolasco Barrientos