Butterknife Required view with ID for field was not found in every fragment

When I try to bind my views with Butterknife I get this IllegalStateException after every time I call ButterKnife.bind(this, fragmentView!!) method.

For example:

//...BindView section
@BindView(R.id.logoutButton)
lateinit var logoutButton: View

@BindView(R.id.closeSession)
lateinit var closeSessionButton: View

@BindView(R.id.settings)
lateinit var settingsButton: View

And I get this:

java.lang.IllegalStateException: Required view login with ID 2131362018 for field logoutButton was not found. If this view is optional add @Nullable (fields) or @Optional (methods) annotation.

The point is that I DIDN'T BIND the 'login' view as the exception says. I bounded 'logutButton'. I did have 'login' view, but in the other fragment which has no connection to fragment where the exception appears.

And this happening in every fragment, activity, or adapter or something else which needs binding views with butterknife. And every time Butterknife tries to bind, an absolutely random view for the field that comes first in declaration order (only first @BindView annotation affected, the next annotation are fine)

I'm using @Nullable annotation and it's helping perfectly (even after I deleted @Nullable annotation - fragment works fine). But I have lots of fragments and activities, so I can't check all of them and add the @Nullable annotation.

How I can find the source of this problem so that it never happens again?

Upvotes: 0

Views: 5602

Answers (4)

xinyu li
xinyu li

Reputation: 1039

Your layout xml file didn't have the widget with id 'R.id.logoutButton', you can quote the id in your Activity maybe because you set that id in other xml layout files, not the one you were using in current activity.

Upvotes: 0

Yyy
Yyy

Reputation: 2335

Try adding this annotation @Nullable or @Optional. I have taken reference from https://github.com/JakeWharton/butterknife/issues/343

@Nullable
@BindView(R.id.tvTitle)

Upvotes: -3

DawidJ
DawidJ

Reputation: 1265

Using Butterknife in kotlin project is pointless. Kotlin already provides direct and easy access to layout and its view. https://kotlinlang.org/docs/tutorials/android-plugin.html

You should stop using butterknife and remove thislibrary from gradle.

Upvotes: 1

This is how i solved this:

Build -> Clean Project

And everything fine!

Upvotes: 7

Related Questions