butterknife 未找到字段 'hello' 的 ID 为 2131427413 的必需视图 'hello'

butterknife Required view 'hello' with ID 2131427413 for field 'hello' was not found

Butterknife我用@bindView来注解Button。 但它发生了一个错误。 Required view 'hello' with ID 2131427413 for field 'hello' was not found.

@BindView(R.id.hello)
Button hello;
@Inject
GitHubApiService gitHubApiService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
    hello.setText("hello");
}

butterknife 的依赖项

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'//这个是自定义注解用的编译插件

buildscript {

    repositories {

        jcenter()

    }

    dependencies {

        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'

    }

}

您需要在调用 ButterKnife.bind(this) 之前设置内容视图,如下所示:

 setContentView(R.layout.your_view);
 ButterKnife.bind(this);

谢谢!我自己找到原因了。

之前我在build.gradle of app module中添加以下内容。 但是下面应该添加到 build.gradle of project

    >classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    >classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'

    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

还要检查 setContentView(R.layout.your_view); 您传递的布局是否正确