Why can't Butterknife find a ViewHolder inside an anonymous class?
Asked Answered
S

0

6

I've got a ListView with an anonymous BaseAdapter:

final ListView myList = (ListView) getActivity().findViewById(R.id.my_list);
myList.setAdapter(new BaseAdapter() {

Inside that anonymous class I have a view holder:

class ViewHolder {
    @InjectView(R.id.textField) TextView text;

    public ViewHolder(View view) {
        ButterKnife.inject(this, view);

        if (text == null) {
            text = (TextView)view.findViewById(R.id.textField);
        }
    }
}

Setting a breakpoint confirms: Butterknife always leaves the text field null, but directly calling findViewById works fine. If I move the ViewHolder class out of the anonymous class, making it a member of my main class, Butterknife works fine. Can someone explain why?

Sanmicheli answered 16/8, 2014 at 19:43 Comment(2)
Try to declare ViewHolder as: static class ViewHolderCake
my hunch is that since it's an anonymous class, ButterKnife doesn't know how to name the generated class, hence it skips it.Jugendstil

© 2022 - 2024 — McMap. All rights reserved.