问题描述
我正在尝试解决旧的Kotlin项目中的问题.但是问题是我无法编译代码.我尝试编译并在Android Studio和IntelliJ中运行.我有同样的错误.
I'm trying to fix an issue in an old kotlin project. But the problem is that I can't compile the code. I tried compile and run in Android Studio and IntelliJ. I got same errors.
以下是错误:
Error:(174, 25) Expression 'length' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found
Error:(176, 60) Unresolved reference: charAt
Error:(148, 67) Expression 'size' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found
Error:(107, 76) Expression 'ordinal' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found
我的gradle脚本:
My gradle script:
buildscript {
ext.kotlin_version = '1.0.4'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.google.gms:google-services:1.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
.
.
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
.
.
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
对于顺序错误:
//enum class
enum class Category(val n:Int, val color:Int, val id : String){
HEADLINE(R.string.category_headline, Color.parseColor("#EC4A42"), "101"),
.
.
}
//where call ordinal func
intent.putExtra(MainActivity.EXTRA_CATEGORY, Category.HEADLINE.ordinal())
对于charAt错误:
For charAt error:
companion object{
fun trim(s : CharSequence) : CharSequence{
var start = 0
var end = s.length()
while (start < end && Character.isWhitespace(s.charAt(start))) {
start++
}
while (end > start && Character.isWhitespace(s.charAt(end - 1))) {
end--
}
return s.subSequence(start, end)
}
}
对于length():
For length():
companion object{
fun trim(s : CharSequence) : CharSequence{
var start = 0
var end = s.length()
while (start < end && Character.isWhitespace(s.charAt(start))) {
start++
}
while (end > start && Character.isWhitespace(s.charAt(end - 1))) {
end--
}
return s.subSequence(start, end)
}
}
size()用法:
class PhotoGalleryAdapter(val ac : Activity, val result : ResponseNewsDetail) : PagerAdapter(){
override fun getCount(): Int = result.gallery!!.size()
.
.
}
任何想法/建议都将不胜感激.干杯!
Any ideas/suggestions would be appreciated. Cheers!
推荐答案
所有这些int返回方法(String#length()
,...)早已成为属性.只需删除括号()
并以属性方式使用它即可.
All of those int-returning methods (String#length()
,...) have some time ago became properties. Just remove parenthesis ()
and use it in properties manner.
var start = 0
var end = s.length //without ()
顺便说一句. String
已经具有方法trim()
btw. String
already has a method trim()
charAt
应该替换为[]
运算符.因此,将s.charAt(end-1)
替换为s[end-1]
charAt
should be replaced with []
operator. So replace s.charAt(end-1)
with s[end-1]
这篇关于Kotlin:类型"Int"的表达式"length"不能作为函数调用.找不到函数"invoke()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!