Butterknife Fragment Injecting Views Not Working?

Aerial picture Aerial · Dec 14, 2013 · Viewed 15.6k times · Source

Can anyone explain why my View element (ListView) is null with the following code:

public class NewsFragment extends Fragment {

    @InjectView(R.id.news_listView) ListView lv;

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (lv == null) {
            UIHelper.showAlert("null?");
        }
    }
}

Am I doing something wrong or is there something wrong with the library, because I pasted the example code to my app to get it working, but it's not working here... Any help is much appreciated!

Answer

Pedro Loureiro picture Pedro Loureiro · Dec 14, 2013

Have you setup your IDE?

IDE CONFIGURATION

Some IDEs require additional configuration in order to enable annotation processing.

IntelliJ IDEA — If your project uses an external configuration (like a Maven pom.xml) then annotation processing should just work. If not, try manual configuration.

Eclipse — Set up manual configuration

Usually this means that the annotation processor didn't do its work. That might be due to misconfiguration or random problems with the IDE. In my experience, every now and then I had to clean the project and build everything again.