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

时间:2018-06-29 08:11:58

标签: android butterknife

Android Studio 3.1, 摇篮4.1, Java 1.8, Android 6.0, 黄油刀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>

在我的活动中

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)

为什么不起作用?

3 个答案:

答案 0 :(得分:2)

XML中没有ID为R.id.noItemsTextView的文本视图。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我遇到了同样的问题,并与之抗争了整整一天 我的ID很好,我的代码也正确,因为我三倍检查了整整一天 然后我发现了一些我从未想过的根本原因

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

更改为

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

问题消失了