@OnClick method from Butterknife crashes app with java.lang.BootstrapMethodError
Asked Answered
N

4

7

I am using Butterknife to simplify my code. I have a TextView in the activity_main.xml file. I want to display a toast by clicking on this TextView But when I use @OnClick annotation from Butterknife library my app crashes

MainActivity.java

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

    @OnClick(R.id.clickme) void clicked() {
        Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
    }

activity_main.xml

<TextView
        android:id="@+id/clickme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click me"/>

Normally it should display a toast but the app crashes with the following stack trace.

2019-01-02 15:26:42.617 3909-3909/com.realestate.app.realestate E/AndroidRuntime: FATAL EXCEPTION: main
**Process: com.realestate.app.realestate, PID: 3909
java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method
    at butterknife.internal.DebouncingOnClickListener.<clinit>(DebouncingOnClickListener.java:12)
    at com.realestate.app.realestate.realestate.MainActivity_ViewBinding.<init>(MainActivity_ViewBinding.java:34)**
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at butterknife.ButterKnife.bind(ButterKnife.java:171)
    at butterknife.ButterKnife.bind(ButterKnife.java:100)
    at com.realestate.app.realestate.realestate.MainActivity.onCreate(MainActivity.java:25)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6680)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.ClassCastException: Bootstrap method returned null
    at butterknife.internal.DebouncingOnClickListener.<clinit>(DebouncingOnClickListener.java:12) 
    **at com.realestate.app.realestate.realestate.MainActivity_ViewBinding.<init>(MainActivity_ViewBinding.java:34)** 
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
    at butterknife.ButterKnife.bind(ButterKnife.java:171) 
    at butterknife.ButterKnife.bind(ButterKnife.java:100) 
    at com.realestate.app.realestate.realestate.MainActivity.onCreate(MainActivity.java:25) 
    at android.app.Activity.performCreate(Activity.java:7136) 
    at android.app.Activity.performCreate(Activity.java:7127) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6680) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

My gradle dependency for Butterknife

implementation 'com.jakewharton:butterknife:9.0.0-rc3'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'
Noisy answered 2/1, 2019 at 10:23 Comment(5)
Have you implemented ButterKnife.bind(this); in your onCreate method?Marucci
Yes sir, I have added that statement. But the error still seem to existNoisy
Please see the new edit. Thank youNoisy
Try cleaning your project (maybe few attempts i.e. 3 to 5 times) as I can't spot anything wrong with your code.Marucci
Downgrade your butterKnife version once, nothing wrong with your codeButacaine
N
2

I solved it by downgrading the library version.

I replaced the following

implementation 'com.jakewharton:butterknife:9.0.0-rc3'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'

with

implementation 'com.jakewharton:butterknife:9.0.0-rc1'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
Noisy answered 7/1, 2019 at 5:48 Comment(0)
V
11

I was facing the same problem. Butter Knife requires that you enable Java 8 in your builds to function as of version 9.0.0 and newer. This is what worked for me ....

app.gradle

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
}

apply plugin: 'com.jakewharton.butterknife'

https://developer.android.com/studio/write/java8-support

Sync your gradle file if you are still facing the problem, clean your project and run again.

Vani answered 1/4, 2019 at 20:10 Comment(1)
I have the same configs as you and it's not working when @OnClick is added.Strophanthus
N
2

I solved it by downgrading the library version.

I replaced the following

implementation 'com.jakewharton:butterknife:9.0.0-rc3'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'

with

implementation 'com.jakewharton:butterknife:9.0.0-rc1'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
Noisy answered 7/1, 2019 at 5:48 Comment(0)
P
0

Maybe there is some other dependency in your build.gradle that transitively pulls in a different version of ButterKnife. Check your dependency tree with gradlew dependencies, and look for different versions of ButterKnife.

If you find that e.g. the com.example:foo dependency pulls in a ButterKnife version different from yours, you can exclude the other ButterKnife version by adding an exclude rule to the foo library:

implementation('com.example:foo:1.2.3') {
    exclude group: 'com.jakewharton', module: 'butterknife'
}

Hopefully this will solve the runtime exception.

Pyne answered 2/1, 2019 at 14:22 Comment(0)
V
0

All answers are partly proper for me. My suggestion is to go to their website and follow the instruction of installation: https://github.com/JakeWharton/butterknife

Valeta answered 2/10, 2019 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.