实施ButterKnife时出现空指针异常

3
我是一个有用的助手,可以为您翻译文本。
我正在尝试在一个小项目中实现ButterKnife库,但是当我的应用程序启动时,它会崩溃。以下是详细信息:
这里是我的MainActivity.java文件:
    package com.example.odai.playwithme;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;

    import butterknife.BindView;
    import butterknife.ButterKnife;

    public class MainActivity extends AppCompatActivity {

        @BindView(R.id.hello_world) TextView hello;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ButterKnife.bind(this);

            hello.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Toast.makeText(getApplicationContext(), "Hello",
                     Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

我的 activity_main.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.odai.playwithme.MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/hello_world"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
   </RelativeLayout>

my build.gradle :

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"

       defaultConfig {
           applicationId "com.example.odai.playwithme"
           minSdkVersion 17
           targetSdkVersion 23
           versionCode 1
           versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
           }
       }
   }

   dependencies {
       compile fileTree(dir: 'libs', include: ['*.jar'])
       testCompile 'junit:junit:4.12'
       compile 'com.android.support:appcompat-v7:23.4.0'
       compile 'com.jakewharton:butterknife:8.0.1'
   }

每当我尝试运行我的应用程序时,都会出现以下错误:
05-22 20:32:52.019 24076-24076/com.example.odai.playwithme E/AndroidRuntime: FATAL EXCEPTION: main
         Process: com.example.odai.playwithme, PID: 24076
         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.odai.playwithme/com.example.odai.playwithme.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
             at android.app.ActivityThread.access$900(ActivityThread.java:150)
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
             at android.os.Handler.dispatchMessage(Handler.java:102)
             at android.os.Looper.loop(Looper.java:168)
             at android.app.ActivityThread.main(ActivityThread.java:5845)
             at java.lang.reflect.Method.invoke(Native Method)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
             at com.example.odai.playwithme.MainActivity.onCreate(MainActivity.java:22)
             at android.app.Activity.performCreate(Activity.java:6248)
             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544) 
             at android.app.ActivityThread.access$900(ActivityThread.java:150) 
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
             at android.os.Handler.dispatchMessage(Handler.java:102) 
             at android.os.Looper.loop(Looper.java:168) 
             at android.app.ActivityThread.main(ActivityThread.java:5845) 
             at java.lang.reflect.Method.invoke(Native Method) 
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

有什么想法吗?


尝试在onCreate方法中使用ButterKnife.inject(this); - Muhammad Waleed
清理项目可能会有所帮助。 - Krupal Shah
ButterKnife的新版本现在使用"bind",而不是"inject"。 - Odai Mohammed
2个回答

4

我之前也遇到过这个问题,最近试了一下,看看是否只是一次性的问题。显然,更新的ButterKnife需要在你的build.gradle文件中添加更多内容:

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'
}

(https://github.com/JakeWharton/butterknife)


0

移除@Bindview行并按照以下步骤正确绑定元素:

  1. 在setContentView行中单击布局名称
  2. 按下alt+enter,它会带来插入选项
  3. 点击生成Butterknife注入
  4. 选择要绑定的元素
  5. 点击确定。 然后它就可以工作了。 您还可以直接从那里设置onclick事件,同时选择要绑定的视图。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接