Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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);
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top