2016-10-08 14 views
0

Butterknife我用@bindView注释按钮。 但它发生了一个错误。 Required view 'hello' with ID 2131427413 for field 'hello' was not found.butterknife所需的视图“你好” ID为2131427413的现场“你好”没有被发现

@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' 

    } 

} 
+1

你没'活动的setContentView' –

+0

你可以尝试清洁工程 –

回答

1

你需要调用ButterKnife.bind(this)像前下方设置内容视图:

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

谢谢你,我发现自己的理由!

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 
    } 
} 
0

此外,检查中setContentView(R.layout.your_view);要传递正确的布局