Butterknife Fragment Injecting Views Not Working?
Asked Answered
D

2

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!

District answered 14/12, 2013 at 18:53 Comment(0)
F
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.

Fraga answered 14/12, 2013 at 19:26 Comment(1)
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.Airboat
A
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);
...
Alberta answered 26/2, 2017 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.