12

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!

JJD
  • 50,076
  • 60
  • 203
  • 339
Aerial
  • 1,185
  • 4
  • 20
  • 42

2 Answers2

12

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.

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • Not working for me =( I followed the steps from the link for Eclipse, but from step # 4 _"Make sure that the .apt_generated/ folder is in your project root."_ - I do not see any folder named `apt-generated` in my project root, and the errors don't go away. – Solace Jul 21 '15 at 05:12
0

This work for me:

...

@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);
...