Android:butterKnife - @BindView - 抛出 NPE
Android: butterKnife - @BindView - throw NPE
Android Studio 2.3.3.
我的布局xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/downloadProgressBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
在我的app/build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.myproject"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
dev {
initWith(debug)
}
}
}
dependencies {
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
annotationProcessor "com.jakewharton:butterknife-compiler:$BUTTER_KNIFE_VERSION"
compile "com.jakewharton:butterknife:$BUTTER_KNIFE_VERSION"
}
这里是 MyActivity 的片段:
public class MyActivityextends extends AppCompatActivity {
@BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pdf_viewer_activity);
ButterKnife.bind(this);
ButterKnife.setDebug(true);
// THROW NPE!!!
downloadProgressBar.setVisibility(View.GONE);
}
}
但是在开始之后 activity 我得到了 NPE:
NullPointerException at com.myproject.MyActivity.onCreate(MyActivity.java:63)
为什么?我想我正确地初始化了 butterKnife 库。 Android Studio 和控制台中的项目成功构建和 运行。
您正在使用 Kotlin,所以您应该这样做
apply plugin: 'com.android.application'
//apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
和
dependencies {
kapt 'com.github.bumptech.glide:compiler:4.2.0'
kapt "com.jakewharton:butterknife-compiler:$BUTTER_KNIFE_VERSION"
compile "com.jakewharton:butterknife:$BUTTER_KNIFE_VERSION"
}
只需要声明变量的类型downloadProgressBar
。
@BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;
改变
@BindView(R.id.downloadProgressBar) downloadProgressBar;
这条线到
@BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;
Android Studio 2.3.3.
我的布局xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/downloadProgressBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
在我的app/build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.myproject"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
dev {
initWith(debug)
}
}
}
dependencies {
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
annotationProcessor "com.jakewharton:butterknife-compiler:$BUTTER_KNIFE_VERSION"
compile "com.jakewharton:butterknife:$BUTTER_KNIFE_VERSION"
}
这里是 MyActivity 的片段:
public class MyActivityextends extends AppCompatActivity {
@BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pdf_viewer_activity);
ButterKnife.bind(this);
ButterKnife.setDebug(true);
// THROW NPE!!!
downloadProgressBar.setVisibility(View.GONE);
}
}
但是在开始之后 activity 我得到了 NPE:
NullPointerException at com.myproject.MyActivity.onCreate(MyActivity.java:63)
为什么?我想我正确地初始化了 butterKnife 库。 Android Studio 和控制台中的项目成功构建和 运行。
您正在使用 Kotlin,所以您应该这样做
apply plugin: 'com.android.application'
//apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
和
dependencies {
kapt 'com.github.bumptech.glide:compiler:4.2.0'
kapt "com.jakewharton:butterknife-compiler:$BUTTER_KNIFE_VERSION"
compile "com.jakewharton:butterknife:$BUTTER_KNIFE_VERSION"
}
只需要声明变量的类型downloadProgressBar
。
@BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;
改变
@BindView(R.id.downloadProgressBar) downloadProgressBar;
这条线到
@BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;