Kotlin:类型为“Int”的表达式“length”无法作为函数调用。找不到函数“invoke()”。

19

我正在尝试修复一个旧的kotlin项目中的问题。但问题是我不能编译代码。我在Android Studio和IntelliJ中尝试了编译和运行,但收到了同样的错误。

以下是出现的错误:

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脚本:

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错误:

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()方法:

 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()
   .
   .
 }

欢迎提出任何想法或建议。谢谢!


只需要修复这些编译错误。API已经变更,找到替代这些函数不应该很难。 - Geralt_Encore
请提供源代码,以便我们查看可能出错的地方。 - rafal
@Geralt_Encore 我是Kotlin的新手。我读到了一些有关弃用API的内容。然后我使用了代码清理工具并手动更改了一些行。但我并没有找到解决这些错误的方法 :( - kdogr
请展示导致错误的代码。 - yole
@rafal 问题已更新。 - kdogr
2个回答

35

所有那些返回 int 值的方法 (String#length(),...) 早就变成了属性。只需删除括号 (),以属性方式使用即可。

    var start = 0
    var end = s.length  //without ()

顺带一提,String 已经有一个 trim() 方法了。

charAt 应该被替换为使用 [] 运算符。所以将 s.charAt(end-1) 替换为 s[end-1]


谢谢,这虽然是小事,但非常重要。'()'让我头疼了,现在已经移除了。 - Yog Guru
在 Kotlin 中,如果您需要访问具有类型 Int 的成员变量,则不使用 getter - Alston

14

Kotlin的表达式getter和setter与Java不同,它们没有括号。

getter: #Class.method setter: #Class.method = value

例如:

从:competitions.value(body?.competitionsList)

到:competitions.value = body?.competitionsList

例如2:

// Gets linearlayout
  val layout: LinearLayout = findViewById(R.id.myLayout)
// Gets the layout params that will allow you to resize the layout
  val params: ViewGroup.LayoutParams = layout.layoutParams
  params.width = 100
  params.height = 100
  layout.layoutParams = params

来源


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接