Android-Butterknife:原因:java.lang.IllegalStateException:未找到所需的视图 xxx

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

Android Studio 3.1, Gradle 4.1, Java1.8, Android 6.0, ButterКnife 8.8.1

我尝试在 android 应用程序上使用 ButterKnife。但是我有一个问题。

build.gradle:

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

app/build.gradle:

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

这是我的 xml 布局: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>

在我的 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);
}

方法showTopAndBottomContainer()成功。但是方法 showNoItems() 不行。 我收到错误:

        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)

为什么它不起作用?

您可能忘记应用 ButterKnife 插件。转到您的 build.gradle 文件并在 下面 apply plugin: 'com.android.library' 添加:apply plugin: 'com.jakewharton.butterknife'

XML 中没有与 ID R.id.noItemsTextView 匹配的文本视图。

遇到同样的问题,折腾了一整天 我的 ID 很好,我的代码也正确,因为我检查了一整天 然后我发现一些我从未想过的事情是根本原因

ButterKnife.bind(this);
setContentView(layoutID());

改为

setContentView(layoutID());
ButterKnife.bind(this);

问题解决了