ArrayList readLines

时间:2018-06-16 11:27:37

标签: android android-studio kotlin

这段代码有什么问题?请帮忙

 val filesDir = filesDir
 val todoFile = File(filesDir, "todo.txt")
 items = ArrayList<String>(FileUtils.readLines(todoFile))

// PS:FileUtils来自(实现'org.apache.commons:commons-io:1.3.2')

这是错误:

(40, 21): None of the following functions can be called with the arguments 
supplied:
public final fun <E> <init>(p0: (MutableCollection<out 
String!>..Collection<String!>?)): kotlin.collections.ArrayList<String> /* = 
java.util.ArrayList<String> */ defined in kotlin.collections.ArrayList
public final fun <E> <init>(p0: Int): kotlin.collections.ArrayList<String> /* 
= java.util.ArrayList<String> */ defined in kotlin.collections.ArrayList

1 个答案:

答案 0 :(得分:1)

此版本的readLines中的

FileUtils会返回List,但Kotlin期望List<String>构造函数中的ArrayList<String>。您可以将结果转换为:

items = ArrayList<String>(FileUtils.readLines(todoFile) as List<String>)

(但是你会收到一个未经检查的强制转换警告),或者你可以使用更新的库版本,其中函数被定义为返回List<String>