Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 607
Nishat sAyyed Avatar asked Jan 02 '19 10:01

Nishat sAyyed


2 Answers

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.

like image 199
Anik Dey Avatar answered Nov 04 '22 02:11

Anik Dey


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'
like image 33
Nishat sAyyed Avatar answered Nov 04 '22 01:11

Nishat sAyyed