androidillegalstateexceptionbutterknife

java.lang.illegalStateException: Butterknife


I am using butterknife to bind my views so when the activity start, the following exception is thrown

java.lang.RuntimeException: Unable to start activity ComponentInfo{..package name...}: java.lang.IllegalStateException: Required view 'l' with ID 2131558524 for field 'tabItem' and method 'check' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.

Note: I have called Butterknife.bind(this) after setContentView(view) and this view is not optional

My Code

public class HandlingActivity extends AppCompatActivity {

@BindView(R.id.container_view)FrameLayout container;
@BindView(R.id.l)TabItem tabItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_handling);
    ButterKnife.bind(this);
}

@OnClick(R.id.l)void check(){
    StoriesFragment storiesFragment = new StoriesFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.container_view,storiesFragment).commit();
     }
}

Solution

  • It is possible if your TabItem is not ready, so try to use this while declaring variable and its respective onclick.

    Taken reference from here

     @Nullable
     @BindView(R.id.l)TabItem tabItem;
    
     @Optional
     @OnClick(R.id.l)
      void check(){
         //method logic...
      }