我第一次使用ButterKnief,按照documentation
我正在尝试绑定我的文本视图并单击按钮更改它的文本,但它不起作用,因为butterknife甚至无法找到视图(获取null)
MainActivity类
public class MainActivity extends AppCompatActivity {
@BindView(R.id.text_view_butter) private TextView text;
static int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Log.d("Tag","");
}
@OnClick(R.id.btn_butterKnife)
public void submit()
{
text.setText("You presed me "+count+" times");
}
}
依赖
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.0.0'
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.worldline.butterknifetest.MainActivity">
<TextView
android:id="@+id/text_view_butter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/btn_butterKnife"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ButterKnife me"
android:layout_below="@+id/text_view_butter"
android:layout_alignParentStart="true"
android:layout_marginTop="77dp" />
</RelativeLayout>
任何想法这里可能缺少什么块?
答案 0 :(得分:1)
您是否已在gradle.build文件中将此行添加到您的依赖项(我没有看到它):
apt 'com.jakewharton:butterknife-compiler:8.0.0'
运行时和编译器现在分为两个工件。
在此处查看更多https://github.com/JakeWharton/butterknife/blob/master/CHANGELOG.md
答案 1 :(得分:1)
将此添加到build.gradle(app)解决了它
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'com.jakewharton:butterknife:8.0.0'
apt 'com.jakewharton:butterknife-compiler:8.0.0'
}