java.lang.illegalStateException: Butterknife

11.9k views Asked by At

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();
     }
}
5

There are 5 answers

2
Aalap Patel On BEST ANSWER

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...
  }
0
ste9206 On
  1. check if you have put in build.gradle (module:app)

    dependencies {
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    provided 'javax.annotation:jsr250-api:1.0'
    compile 'com.jakewharton:butterknife:8.5.1'
    
    }
    
  2. check if your id's exists

  3. try to add @OnClick(R.id.l)void check(View v){ ... }

0
Jorge Casariego On

In my case this error was because I was using two layouts for different versions:

  • activity_login.xml

  • activity_login.xml (v21)

I added a progressBar in activity_login.xml but...

I forgot to added to activity_login.xml (v21).

0
GFPF On

It's working for me after add the annotation @Optional to @Onclick in my Activity class:

@Optional
@OnClick({R.id.your_id_1, R.id.your_id_2})
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.your_id_1:
            break;
        case R.id.your_id_2:
            break;
    }
}

Import the dependencies and bind your Activity:

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Optional;

public class YourActivity extends AppCompatActivity {
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
        ButterKnife.bind(this);
    }
}

The Butter Knife GitHub README provides instructions:

To use Butter Knife in a library, add the plugin to your buildscript:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
  }
}

and to your app/build.gradle

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId 'your_app_id'
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

    }
}

dependencies {
        implementation 'com.jakewharton:butterknife:8.8.1'
        annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
1
Bakavani On

I had similar error, it was due to wrong layout file was being inflated on the view which didnt have the field in question that was being reported in error.

Related Questions in ILLEGALSTATEEXCEPTION