Search code examples
androidbutterknife

Android-Butterknife: Caused by: java.lang.IllegalStateException: Required view xxx was not found


Android Studio 3.1, Gradle 4.1, Java 1.8, Android 6.0, ButterКnife 8.8.1

I try to use ButterKnife on android app. But I have a problem.

in build.gradle:

classpath 'com.android.tools.build:gradle:3.1.2'

in app/build.gradle:

implementation "com.jakewharton:butterknife:8.8.1"
kapt "com.jakewharton:butterknife-compiler:8.8.1"

Here my xml layout: offer_details_pdf.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/offerDetailsToolBarMainContainer"
        layout="@layout/offer_details_top_container"
        android:layout_width="0dp"
        android:layout_height="56dp"/>

    <TextView
        android:id="@+id/noItemsTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/no_items"
        android:visibility="gone"/>  

</android.support.constraint.ConstraintLayout>

In my activity

public class OfferDetailsPdfActivity extends AppCompatActivity implements MyInterface {
    @BindView(R.id.offerDetailsToolBarMainContainer)
    ConstraintLayout offerDetailsToolBarMainContainer;
    @BindView(R.id.noItemsTextView)
    TextView noItemsTextView;    

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    setContentView(R.layout.offer_details_pdf);
    ButterKnife.bind(this);
}        

// implment method of interface MyInterface
@Override
public void showTopAndBottomContainer() {
   offerDetailsToolBarMainContainer.setVisibility(View.VISIBLE);
}

// implment method of interface MyInterface
@Override
public void showNoItems() {
  noItemsTextView.setVisibility(View.VISIBLE);
}

Method showTopAndBottomContainer() success work. But method showNoItems() not. I get error:

        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.android.customer.debug/com.myproject.android.customer.ui.OfferDetailsPdfActivity}: java.lang.IllegalStateException: Required view 'noItemsTextView' with ID 2131296554 for field 'noItemsTextView' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        Caused by: java.lang.IllegalStateException: Required view 'noItemsTextView' with ID 2131296554 for field 'noItemsTextView' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
com.myproject.android.customer.ui.OfferDetailsPdfActivity_ViewBinding.<init>(OfferDetailsPdfActivity_ViewBinding.java:73)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at butterknife.ButterKnife.createBinding(ButterKnife.java:199)
            at butterknife.ButterKnife.bind(ButterKnife.java:124)

Why it's not working?


Solution

  • There is no text view matching with id R.id.noItemsTextView in the XML.