0

I am using ButterKnife 8.0.1 in my android studio project. Below is my gradle file and snippet of fragment class. I am unable to see Toast message in button click. But I am able to see Toast if I use onCLicklistener .

Please help me find out what I am doing wrong I am stuck

My Gradle

apply plugin: 'com.android.application'


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "tdd.serveroverload.com.tdd"
        minSdkVersion 15
        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.android.support:design:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.jakewharton:butterknife:8.0.1'
}

Fragment class

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View calcView = inflater.inflate(R.layout.content_main, container, false);

        calcView.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 //It Works
                Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
            }
        });

        unbinder = ButterKnife.bind(this, calcView);

        return calcView;
    }


    @OnClick(R.id.one)
    public void one(View view) {

         //It Does not Works
        if (view.getId() == R.id.one) {

            Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
        }
        Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
        result.setText(result.getText().toString() + 1);
    }
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154

4 Answers4

7

From the Butterknife github page:

Add this to you project-level build.gradle:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

Add this to your module-level build.gradle:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

Make sure the line apply plugin ... is placed somewhere at the top of the file.

Dharman
  • 30,962
  • 25
  • 85
  • 135
MidasLefko
  • 4,499
  • 1
  • 31
  • 44
  • Thanks I did it but now I am getting crash Caused by: java.lang.RuntimeException: Unable to bind views for tdd.serveroverload.com.tdd.ui.fragment.HomeFragment at butterknife.ButterKnife.bind(ButterKnife.java:189) at butterknife.ButterKnife.bind(ButterKnife.java:167) at tdd.serveroverload.com.tdd.ui.fragment.HomeFragment.onCreateView(HomeFragment.java:85) – Hitesh Sahu Jun 07 '16 at 07:30
  • It solved all my problem thanks MidasLefko for help. – Hitesh Sahu Jun 07 '16 at 07:39
  • My pleasure I ran into the same issue when I upgraded from 7.0.1 to 8.0.1 – MidasLefko Jun 07 '16 at 07:52
1

To bind click event on fragment view :

You just need to add an annotation. No need to check id using a view.

Please update your code as below :

 @OnClick(R.id.one)
    public void one() {
        Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
        Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
        result.setText(result.getText().toString() + 1);
    }

Here is sample code from below demo :

    /**
     * Attempts to call below method when 'Fragment Binding Button' get clicked.
     */
    @OnClick(R.id.btn_fragment_binding)
    void OnFragmentBindingClicked() {
        loadSampleFragment();
    }

For more information and different binding please check below demo :

https://github.com/mamatagelanee07/ButterKnifeDemo

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66
  • I know adding id is optional but it was not working without it so I started trying with id, you can see second toast I had added . – Hitesh Sahu Jun 07 '16 at 07:44
1
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'

    }
}
V.Soni
  • 99
  • 1
  • 6
0

Same website https://github.com/JakeWharton/butterknife#download Please add same below

dependencies {
  compile 'com.jakewharton:butterknife:8.6.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}