Butterknife 的@OnClick 方法使应用程序崩溃 java.lang.BootstrapMethodError
@OnClick method from Butterknife crashes app with java.lang.BootstrapMethodError
我正在使用 Butterknife
来简化我的代码。
我在 activity_main.xml file
中有一个 TextView
。
我想通过点击这个 TextView
来祝酒词
但是当我使用 Butterknife
库中的 @OnClick
注释时,我的应用程序崩溃了
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"/>
通常它应该显示一个 toast,但应用程序崩溃并显示以下堆栈跟踪。
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)
我对 Butterknife
的 gradle 依赖
implementation 'com.jakewharton:butterknife:9.0.0-rc3'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'
也许您的 build.gradle 中存在一些其他依赖项,它们会传递引入不同版本的 ButterKnife。使用 gradlew dependencies
检查您的依赖关系树,并寻找不同版本的 ButterKnife。
如果你发现,例如com.example:foo
依赖项引入了与您不同的 ButterKnife 版本,您可以通过向 foo
库添加 exclude
规则来排除其他 ButterKnife 版本:
implementation('com.example:foo:1.2.3') {
exclude group: 'com.jakewharton', module: 'butterknife'
}
希望这能解决运行时异常。
我通过降级库版本解决了
我替换了下面的
implementation 'com.jakewharton:butterknife:9.0.0-rc3'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'
和
implementation 'com.jakewharton:butterknife:9.0.0-rc1'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
我遇到了同样的问题。 Butter Knife 要求您在构建中启用 Java 8 才能在 9.0.0 及更新版本中运行。这对我有用....
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
如果您仍然遇到问题,请同步您的 gradle 文件,清理您的项目并再次 运行。
所有答案都部分适合我。我的建议是访问他们的网站并按照安装说明进行操作:https://github.com/JakeWharton/butterknife
我正在使用 Butterknife
来简化我的代码。
我在 activity_main.xml file
中有一个 TextView
。
我想通过点击这个 TextView
来祝酒词
但是当我使用 Butterknife
库中的 @OnClick
注释时,我的应用程序崩溃了
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"/>
通常它应该显示一个 toast,但应用程序崩溃并显示以下堆栈跟踪。
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)
我对 Butterknife
implementation 'com.jakewharton:butterknife:9.0.0-rc3'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'
也许您的 build.gradle 中存在一些其他依赖项,它们会传递引入不同版本的 ButterKnife。使用 gradlew dependencies
检查您的依赖关系树,并寻找不同版本的 ButterKnife。
如果你发现,例如com.example:foo
依赖项引入了与您不同的 ButterKnife 版本,您可以通过向 foo
库添加 exclude
规则来排除其他 ButterKnife 版本:
implementation('com.example:foo:1.2.3') {
exclude group: 'com.jakewharton', module: 'butterknife'
}
希望这能解决运行时异常。
我通过降级库版本解决了
我替换了下面的
implementation 'com.jakewharton:butterknife:9.0.0-rc3'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc3'
和
implementation 'com.jakewharton:butterknife:9.0.0-rc1'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
我遇到了同样的问题。 Butter Knife 要求您在构建中启用 Java 8 才能在 9.0.0 及更新版本中运行。这对我有用....
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
如果您仍然遇到问题,请同步您的 gradle 文件,清理您的项目并再次 运行。
所有答案都部分适合我。我的建议是访问他们的网站并按照安装说明进行操作:https://github.com/JakeWharton/butterknife