android - Butter Knife 8.8.1 NullPointerException

时间:2017-09-08 14:05:51

标签: android android-layout butterknife

我正在使用butterknife:8.8.1就像提到here

一样

我的项目级别gradle如下所示

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
        ...

    }
}
apply plugin: 'com.jakewharton.butterknife'

模块级别gradle如下所示

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
dependencies {
    ...    
    compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}

活动文件如下所示

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details);
    ButterKnife.bind(this);
    ButterKnife.setDebug(true);
}

xml文件如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/device_info_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/sensor_settings_background">

    ....
   <RelativeLayout
        android:id="@+id/container_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@color/settings_background">

        <Button
            android:id="@+id/connect_button"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_alignParentLeft="true"
            android:background="@drawable/selector"
            android:text="@string/connect"
            android:textColor="@color/red"
            android:textStyle="italic" />

        <Button
            android:id="@+id/register_button"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="4dp"
            android:text="@string/register"
            android:visibility="gone" />

        <Button
            android:id="@+id/unregister_button"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/selector"
            android:text="@string/unregister"
            android:textColor="@color/red"
            android:textStyle="italic"
            android:visibility="gone"
            tools:visibility="visible" />
    </RelativeLayout>
</RelativeLayout>

编译后,使用Resource Ids生成R2.java文件 R2.java res idresId not binding with view张图片。

我收到了NullPointerException

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setClickable(boolean)' on a null object reference

有人可以帮助我理解我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

我确实遇到过同样的问题。 将Button更改为AppCompatButton,问题已解决。

<android.support.v7.widget.AppCompatButton
    android:id="@+id/connect_button"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:layout_alignParentLeft="true"
    android:background="@drawable/selector"
    android:text="@string/connect"
    android:textColor="@color/red"
    android:textStyle="italic" />