【发布时间】:2016-09-25 09:19:53
【问题描述】:
我正在尝试使用 Butterknife 绑定 TextView 我的主要活动和布局由 ID 为 tv_app_name 的 TextView 组成。我从documentation 关注。
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv_app_name)
TextView tvAppName;
@BindString(R.string.app_name)
String appName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ButterKnife.setDebug(true);
tvAppName.setText(appName);
}
}
我遇到了一个问题,即 tvAppName 未初始化并显示 nullPointerException。我添加了 ButterKnife.setDebug(true); 正如 StackOverflow 的一些答案中所建议的那样,我在日志中发现了 ButterKnife: Not found. Trying superclass android.support.v7.app.AppCompatActivity。
在我寻找解决此问题的方法时。有人说添加依赖butterknife-compiler,但我已经添加了:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
//butterknife
compile 'com.jakewharton:butterknife:8.4.0'
apk 'com.jakewharton:butterknife-compiler:8.4.0'
}
什么是问题bihinde:
黄油刀:未找到。尝试超类 android.support.v7.app.AppCompatActivity 以及如何解决这个问题?
【问题讨论】:
标签: android butterknife null-pointer