gpt4 book ai didi

android - android 中的 Lambda 表达式

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:32 24 4
gpt4 key购买 nike

如何在android中使用lambda表达式?例如,我在 IntelliJ IDEA 中编译这段代码:

package com.example.myapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
test s = () -> {return "Lambda expressions test";};
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Lambda expression")
.setMessage(s.t())
.create();
alertDialog.show();
}
}
interface test {
public String t();
}

但是有这个错误:

Information:Using javac 1.8.0_05 to compile java sources
Information:36 errors
Information:0 warnings
Information:Compilation completed with 36 errors and 0 warnings in 29 sec
Error:Android Dex: [myappі] UNEXPECTED TOP-LEVEL EXCEPTION:
Error:Android Dex: [myappі] com.android.dx.cf.iface.ParseException: InvokeDynamic not supported

如何设置才能使用 lambda 表达式?

最佳答案

Android 仅支持 java 6 和 7。您可以使用插件获取 lambda,例如 https://github.com/evant/gradle-retrolambda .

关于android - android 中的 Lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24211333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com