Butterknife is not initializing views in AOSP marshmallow but works fine in lollipop

348 Views Asked by At

I am using butterknife library in my project. It works fine in when I build in Lollipop AOSP but the same code initializes nothing and so after that NPE is thrown in Marshmallow AOSP.

Here is the code:

public class ErrorLayoutViewHolder extends ViewHolder {

    @Bind(R.id.error_content_container)
    View errorContentLayout;
    @Bind(R.id.error_message)
    TextView errorMessage;
    @Bind(R.id.retry_button)
    View retryButton;

    public ErrorLayoutViewHolder(final View itemView, View.OnClickListener retryListener) {
        super(itemView);
        retryButton.setOnClickListener(retryListener);
    }
}

This is the parent class ViewHolder.java:

public class ViewHolder {
    final public View itemView;

    public ViewHolder(View itemView) {
        this.itemView = itemView;
        itemView.setTag(this);
        ButterKnife.bind(this, itemView);
    }
}

This is the stacktrace:

AndroidRuntime: java.lang.NullPointerException: Attempt to invoke     virtual method 'void  android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
02-25 20:38:57.538  5922  5922 E AndroidRuntime:    at com.micromax.aroundyou.model.ui.viewholders.ErrorLayoutViewHolder.<init>(ErrorLayoutViewHolder.java:38)

Line 38:

 retryButton.setOnClickListener(retryListener);

What can be the problem?

1

There are 1 best solutions below

4
On

I think you should cast the view to a button or whatever custom view you are using. Did you try to use Butterknife's

@OnClick(R.id.retry_button) void foo(){ //TODO button logic }

? This way you do not care about the class of the view at all.