Reputation: 819
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();
}
}
Upvotes: 3
Views: 12081
Reputation: 949
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'
}
Upvotes: 0
Reputation: 131
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.
Upvotes: 2
Reputation: 22212
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).
Upvotes: 2
Reputation: 1892
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'
}
check if your id's exists
try to add @OnClick(R.id.l)void check(View v){ ... }
Upvotes: 0
Reputation: 2076
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...
}
Upvotes: 9