【问题标题】:Android: butterKnife - @BindView - throw NPEAndroid:butterKnife - @BindView - 抛出 NPE
【发布时间】:2017-10-22 15:43:25
【问题描述】:

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 的 sn-p:

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); 
     }    
}

但是在开始这个活动后我得到了 NPE:

NullPointerException at com.myproject.MyActivity.onCreate(MyActivity.java:63)

为什么?我想我更正了初始化 butterKnife 库。从 Android Studio 和控制台成功构建和运行项目。

【问题讨论】:

  • 当你写 @BindView(R.id.downloadProgressBar) downloadProgressBar; 你的意思是 @BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar; 对吧?
  • 是的,我编辑我的帖子

标签: android nullpointerexception butterknife


【解决方案1】:

你正在使用 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" 
}

【讨论】:

    【解决方案2】:

    只需要声明变量downloadProgressBar的类型即可。

    @BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;

    【讨论】:

      【解决方案3】:

      改变

      @BindView(R.id.downloadProgressBar) downloadProgressBar;
      

      这一行到

      @BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;
      

      【讨论】:

        猜你喜欢
        • 2021-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 2013-06-14
        • 2022-01-12
        • 1970-01-01
        相关资源
        最近更新 更多