在Android项目中,我尝试使用Java 8的新lambda功能,因此我将现有的正常运行的匿名内部类修改为lambda。但是,使用以下错误消息执行代码时应用程序崩溃:
11-25 11:20:33.485 27105-27105/com.infor.Dashboards.dev E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.infor.Dashboards.dev, PID: 27105
java.lang.VerifyError: Verifier rejected class com.infor.Dashboards.Settings.DebugTestingFragment$3$1$$Lambda$-void_onPostExecute_java_lang_String_string_LambdaImpl0 due to bad method void com.infor.Dashboards.Settings.DebugTestingFragment$3$1$$Lambda$-void_onPostExecute_java_lang_String_string_LambdaImpl0.processMessage(android.os.Message) (declaration of 'com.infor.Dashboards.Settings.DebugTestingFragment$3$1$$Lambda$-void_onPostExecute_java_lang_String_string_LambdaImpl0' appears in /data/app/com.infor.Dashboards.dev-1/base.apk)
at com.infor.Dashboards.Settings.DebugTestingFragment$3$1.onPostExecute(DebugTestingFragment.java:308)
at com.infor.Dashboards.Settings.DebugTestingFragment$3$1.onPostExecute(DebugTestingFragment.java:304)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5460)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
以下是示例代码(单击按钮时应用程序崩溃):
view.findViewById(R.id.test_code).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Void, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String string) {
super.onPostExecute(string);
ApplicationState.PAUSE_HANDLER.sendMessage(new Message(), message -> {
Logger.LogDebug(string + " " + v.getId());
}, "test");
}
@Override
protected String doInBackground(Void... params) {
return "test";
}
}.execute((Void) null);
}
});
我设法发现问题是lambda访问&#34; v&#34;变量保持由onClick()方法作为参数给出的视图。将其更改为&#34; final&#34;没有任何效果。但是,当它被复制到lambda上方时,它可以工作:
view.findViewById(R.id.test_code).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Void, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String string) {
super.onPostExecute(string);
View v1 = v;
ApplicationState.PAUSE_HANDLER.sendMessage(new Message(), message -> {
Logger.LogDebug(string + " " + v1.getId());
}, "test");
}
@Override
protected String doInBackground(Void... params) {
return "test";
}
}.execute((Void) null);
}
});
问题是: