Toni
Toni

Reputation: 165

butterknife @OnClick not working

I've follow some of the solutions here for my problem but none of them worked. I implemented @OnClick using Butterkinfe but when I click it is not working. Moreover, if I try to implement the listener manually using setOnClickListener, it shows a null reference on bLoadDeliveres, as if the Butterknife did not find it.

Here's the xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".view.activity.LoginActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bLoadDeliveries"
        android:text="LOAD DELIVERIES" />
</LinearLayout>

If I do it like this, it just works so the layout is ok: This is OKAY

Button bLoadDeliveries;

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

        bLoadDeliveries = (Button) findViewById(R.id.bLoadDeliveries);

        if(bLoadDeliveries == null)
            Toast.makeText(LoginActivity.this, "Null", Toast.LENGTH_LONG).show();
        else {
            bLoadDeliveries.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    navigator.navigateToDeliveryList(context);
                }
            });
        }
    }

Using Butterknife, Compilation and run work just fine but when I click the button it does nothing, like the connection between the Button and the @OnClick is not done at all. Why is it null? Why is not connected? Any ideas? Thanks for the help!

This is NOT OKAY

public class LoginActivity extends BaseActivity {

    @BindView(R.id.bLoadDeliveries)
    Button bLoadDeliveries;

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

    // Goes to the user list screen.    
    @OnClick(R.id.bLoadDeliveries)
    void navigateToUserList() {

        Toast.makeText(LoginActivity.this, "Hola", Toast.LENGTH_SHORT).show();
        this.navigator.navigateToDeliveryList(this);
    }
}

EDITED : Dependency files

apply from: 'buildsystem/ci.gradle'
apply from: 'buildsystem/dependencies.gradle'

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath "com.fernandocejas.frodo:frodo-plugin:0.8.3"
    }
}
allprojects {

    ext {
        androidApplicationId = 'es.app.test.presentation'
        androidVersionCode = 1
        androidVersionName = "1.0"
        testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
        testApplicationId = 'es.app.test.presentation.test'
    }
}

task wrapper(type: Wrapper) {
    description 'Creates the gradle wrapper.'
    gradleVersion '2.12'
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

dependencies.gradle

allprojects {
  repositories {
    jcenter()
  }
}

ext {
  //Android
  androidBuildToolsVersion = "24.0.1"
  androidMinSdkVersion = 23
  androidTargetSdkVersion = 24
  androidCompileSdkVersion = 24

  //Libraries
  daggerVersion = '2.5'
  butterKnifeVersion = '8.2.1'
  recyclerViewVersion = '24.1.1'
  rxJavaVersion = '1.1.8'
  rxAndroidVersion = '1.2.1'
  javaxAnnotationVersion = '1.0'
  javaxInjectVersion = '1'
  gsonVersion = '2.4'
  okHttpVersion = '3.4.1'
  androidAnnotationsVersion = '24.1.1'
  sqliteAssetHelperVersion = '2.0.1'

  //Testing
  robolectricVersion = '3.0'
  jUnitVersion = '4.12'
  assertJVersion = '1.7.1'
  mockitoVersion = '1.9.5'
  dexmakerVersion = '1.0'
  espressoVersion = '2.0'
  testingSupportLibVersion = '0.1'

  //Development
  leakCanaryVersion = '1.3.1'

  presentationDependencies = [
      daggerCompiler:     "com.google.dagger:dagger-compiler:${daggerVersion}",
      dagger:             "com.google.dagger:dagger:${daggerVersion}",
      butterKnife:        "com.jakewharton:butterknife:${butterKnifeVersion}",
      recyclerView:       "com.android.support:recyclerview-v7:${recyclerViewVersion}",
      rxJava:             "io.reactivex:rxjava:${rxJavaVersion}",
      rxAndroid:          "io.reactivex:rxandroid:${rxAndroidVersion}",
      javaxAnnotation:    "javax.annotation:jsr250-api:${javaxAnnotationVersion}",
  ]

  presentationTestDependencies = [
      mockito:            "org.mockito:mockito-core:${mockitoVersion}",
      dexmaker:           "com.google.dexmaker:dexmaker:${dexmakerVersion}",
      dexmakerMockito:    "com.google.dexmaker:dexmaker-mockito:${dexmakerVersion}",
      espresso:           "com.android.support.test.espresso:espresso-core:${espressoVersion}",
      testingSupportLib:  "com.android.support.test:testing-support-lib:${testingSupportLibVersion}",
  ]

  domainDependencies = [
      daggerCompiler:     "com.google.dagger:dagger-compiler:${daggerVersion}",
      dagger:             "com.google.dagger:dagger:${daggerVersion}",
      javaxAnnotation:    "javax.annotation:jsr250-api:${javaxAnnotationVersion}",
      javaxInject:        "javax.inject:javax.inject:${javaxInjectVersion}",
      rxJava:             "io.reactivex:rxjava:${rxJavaVersion}",
  ]

  domainTestDependencies = [
      junit:              "junit:junit:${jUnitVersion}",
      mockito:            "org.mockito:mockito-core:${mockitoVersion}",
  ]

  dataDependencies = [
      daggerCompiler:     "com.google.dagger:dagger-compiler:${daggerVersion}",
      dagger:             "com.google.dagger:dagger:${daggerVersion}",
      okHttp:             "com.squareup.okhttp3:okhttp:${okHttpVersion}",
      gson:               "com.google.code.gson:gson:${gsonVersion}",
      rxJava:             "io.reactivex:rxjava:${rxJavaVersion}",
      rxAndroid:          "io.reactivex:rxandroid:${rxAndroidVersion}",
      javaxAnnotation:    "javax.annotation:jsr250-api:${javaxAnnotationVersion}",
      javaxInject:        "javax.inject:javax.inject:${javaxInjectVersion}",
      androidAnnotations: "com.android.support:support-annotations:${androidAnnotationsVersion}",
      sqliteAssetHelper:  "com.readystatesoftware.sqliteasset:sqliteassethelper:${sqliteAssetHelperVersion}"
  ]

  dataTestDependencies = [
      junit:              "junit:junit:${jUnitVersion}",
      assertj:            "org.assertj:assertj-core:${assertJVersion}",
      mockito:            "org.mockito:mockito-core:${mockitoVersion}",
      robolectric:        "org.robolectric:robolectric:${robolectricVersion}",
  ]

  developmentDependencies = [
      leakCanary: "com.squareup.leakcanary:leakcanary-android:${leakCanaryVersion}",
  ]
}

presentation module

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'com.fernandocejas.frodo'

frodo {
    enabled = true
}

android {
    def globalConfiguration = rootProject.extensions.getByName("ext")

    compileSdkVersion globalConfiguration.getAt("androidCompileSdkVersion")
    buildToolsVersion globalConfiguration.getAt("androidBuildToolsVersion")


    defaultConfig {

        minSdkVersion globalConfiguration.getAt("androidMinSdkVersion")
        targetSdkVersion globalConfiguration.getAt("androidTargetSdkVersion")

        applicationId globalConfiguration.getAt("androidApplicationId")
        versionCode globalConfiguration.getAt("androidVersionCode")
        versionName globalConfiguration.getAt("androidVersionName")
        testInstrumentationRunner globalConfiguration.getAt("testInstrumentationRunner")
        testApplicationId globalConfiguration.getAt("testApplicationId")

    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
    }

    lintOptions {
        quiet true
        abortOnError false
        ignoreWarnings true
        disable 'InvalidPackage'            //Some libraries have issues with this.
        disable 'OldTargetApi'              //Lint gives this warning but SDK 20 would be Android L Beta.
        disable 'IconDensities'             //For testing purpose. This is safe to remove.
        disable 'IconMissingDensityFolder'  //For testing purpose. This is safe to remove.
    }

    signingConfigs {
        debug {
            storeFile file('../buildsystem/debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }


    /*   buildTypes {
           release {
               minifyEnabled false
               proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
           }
       }
   */

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
    }
}

dependencies {

    def presentationDependencies = rootProject.ext.presentationDependencies
    def presentationTestDependencies = rootProject.ext.presentationTestDependencies
    def developmentDependencies = rootProject.ext.developmentDependencies

    compile project(':domain')
    compile project(':data')

    compile 'com.android.support:appcompat-v7:24.1.1'

    apt presentationDependencies.daggerCompiler
    compile presentationDependencies.dagger
    compile presentationDependencies.butterKnife
    apt presentationDependencies.butterKnife
    compile presentationDependencies.recyclerView
    compile presentationDependencies.rxJava
    compile presentationDependencies.rxAndroid
    provided presentationDependencies.javaxAnnotation

    androidTestCompile presentationTestDependencies.mockito
    androidTestCompile presentationTestDependencies.dexmaker
    androidTestCompile presentationTestDependencies.dexmakerMockito
    androidTestCompile presentationTestDependencies.espresso
    androidTestCompile presentationTestDependencies.testingSupportLib

    //Development
    compile developmentDependencies.leakCanary

Upvotes: 2

Views: 1532

Answers (1)

MidasLefko
MidasLefko

Reputation: 4559

I looked at your original post (pre-edit) and it seems that in your presentation module gradle file you have apply plugin: 'com.neenbedankt.android-apt' That should be apply plugin: 'android-apt' (see the butterknife readme.md).

Also, do you have this line in your gradle anywhere: apt 'com.jakewharton:butterknife-compiler:8.2.1'? I see this: compile presentationDependencies.butterKnife' but wouldn't that turn into apt "com.jakewharton:butterknife:8.2.1" when the script is run (ie missing the "-compiler")?

EDITED

In your dependencies.gradle, in the presentationDependencies add the following line:

presentationDependencies = [    
    butterKnifeApt:        "com.jakewharton:butterknife-compiler:${butterKnifeVersion}",
]

Then in your presentation module modify:

apt presentationDependencies.butterKnife 

to:

apt presentationDependencies.butterKnifeApt

Upvotes: 1

Related Questions