コードを簡略化するために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"/>
通常はトーストが表示されますが、アプリは次のスタックトレースでクラッシュします。
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'
すべての答えは部分的に私にとって適切です。私の提案は、彼らのウェブサイトに行き、インストールの指示に従うことです: https://github.com/JakeWharton/butterknife