ButterKnife 8.0.1 不工作

ButterKnife 8.0.1 not working

我正在使用 butterknife 8.0.1,但出现 nullpointerexception

这一行在我的 build.grade 文件中: compile 'com.jakewharton:butterknife:8.0.1'

这是我的Main Class:(我正确地写了包含)

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.MainScreenTextView) TextView mainText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        **mainText.setText("Butter knife is working fine");**
    }

这是MainActivity.xml:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <TextView
        android:id="@+id/MainScreenTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is the Main Screen"
        android:textColor="#000000"
        android:background="#666666"
        android:padding="5dp"
        android:textSize="20dp"/>
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

根据 the readme,您需要包含 butterknife-compiler 以便自动生成生成的代码:

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.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

如果没有这个,就不会加载生成的代码,因此 none 个字段会被设置。

您可以通过调用 ButterKnife.setDebug(true) 并查看 Logcat.

来验证 ButterKnife 是否正常工作
App Level(build.gradle)

apply plugin: 'android-apt'
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}


Project Level(build.gradle)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {

        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

我在Fragment中用过这个库,有NPE。我的代码是:

ButterKnife.bind(view);

但是错了。库需要知道两个对象:
1) 目标 - 带注解 @BindView
2) 来源 - 有浏览量

这样写就对了:

ButterKnife.bind(this, view);

当这个 - 你的片段,和视图 - 这个片段的视图。

来自 JakeWharton

Yes that plugin is no longer needed. You're already using annotationProcessor for the 'butterknife-compiler' artifact which is built-in to the Android Gradle plugin.

那么解决办法就是删除apt类路径'com.neenbedankt.gradle.plugins:android-apt:1.8'

我也有这个问题,只是因为我从 Android Studio 的依赖管理中添加了 butterknife,而不是从 Butterknife 网站复制粘贴 gradle 行。 所以我不得不添加 compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' 而不仅仅是 compile 'com.jakewharton:butterknife:8.5.1'

在你的 gradle 文件中添加 annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' 它对我有用

对我来说,问题是我使用的是

annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'

而不是

 apt 'com.jakewharton:butterknife-compiler:8.7.0

在将我的项目更新到 Gradle 版本 3.0.1 之后,我刚刚遇到了这个问题。我能够通过在 Gradle app 文件中包含以下行来修复它:

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

最后的结果是:

    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.jakewharton:butterknife:8.8.1'
        annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    }

我希望这对某人有所帮助,尽管这个问题很老。

像这样在 build.gradle 文件上配置 Butterknife,

compile("com.jakewharton:butterknife:8.5.1")
annotationProcessor "com.jakewharton:butterknife-compiler:8.5.1"

对我有用。

如果你使用科特林:

确保在模块应用程序中使用此依赖项:

dependencies {
   implementation 'com.jakewharton:butterknife:10.0.0'
   kapt 'com.jakewharton:butterknife-compiler:10.0.0'
}