问题描述
我已经安装了ButterKnife
我的build.gradle
看起来像这样:
I've installed ButterKnife
my build.gradle
looks like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.jakewharton:butterknife:8.4.0'
}
我的loginActivity看起来像这样:
My loginActivity looks like this:
package com.example.egen.forum;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();
}
@OnClick(R.id.btnLogin) public void test() {
Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();
}
}
第二个吐司不出现.我在这里做错什么了?
The second toast does not show up. What am I doing wrong here?
推荐答案
您尚未包括用于ButterKnife代码生成的注释处理器.像 GitHub页面中所述:
You haven't included annotation processor for ButterKnife code generation. Do it like described on the GitHub page:
dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
并应用插件:
apply plugin: 'com.jakewharton.butterknife'
否则,您的代码看起来不错.
Otherwise, your code looks fine.
说明:ButterKnife库使用注释处理器生成用于提供对视图的引用的代码,并执行ButterKnife注释的方法.如果您重建项目,而AndroidStudio显示未使用@OnClick
注释的方法,则可能是错误的.如果提供了注释处理器并且可以正常工作,则注释处理器应该显示为已使用并导致生成方法.
Explanation: ButterKnife library uses annotation processor for generating the code that provides the references to views and executes ButterKnife annotated methods. If you rebuild your project, and the AndroidStudio shows that the @OnClick
annotated method is unused, then somethings wrong. If the annotation processor is provided and works correctly, it should show as used and lead to a generated method.
这篇关于Android ButterKnife的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!