0

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?

binay37
  • 121
  • 1
  • 11

1 Answers1

0

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.

  • But when I remove the button and try to change the visibility of textview (errorMessage), it also throws NPE – binay37 Feb 25 '16 at 15:44
  • Did you check if the errorMessage is not null when you try to display it? If you are sure that it is not null then try this : `errorMessage.setText(String.valueOf(yourerror))`. If this is not the issue then check your layout and make sure you have the views of everything you try to bind. – Konstantinos Michael Feb 25 '16 at 15:48
  • Problem is that this code works fine in lollipop aosp but not in marshmallow aosp – binay37 Feb 25 '16 at 19:17
  • 1
    Recently, I found that jack compiler is used in marshmallow and so butterknife is not working. I did the same thing in android studio gradle file. I added a line: useJack true. How this problem can be solved? – binay37 Mar 04 '16 at 21:52