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.

Here are the 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

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

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

对于长度():

 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!

推荐答案

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

btw. String already has a method trim()

charAt should be replaced with [] operator. So replace s.charAt(end-1) with s[end-1]

Kotlin相关问答推荐

Jetpack Compose中的数字 Select 器问题

可选的.在kotlin中不使用泛型参数

Kotlin:类型不匹配:推断的类型已运行,但应等待

在 Kotlin 中将两个字节转换为 UIn16

找不到有效的 Docker 环境

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

Kotlin:使用另一个列表和字母顺序对列表进行排序的有效方法

如何在 micronaut 上启用 swagger UI?

如何判断给定字符串是否多次包含另一个子字符串?

Kotlin 中获取类简单名称的最佳实践

从字符串列表构建字符串

Kotlin:不允许在辅助构造函数参数上使用val

添加 Kapt 插件后 - 执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障

在 Spring Framework 5.1 中注册具有相同名称的测试 bean

如何使 TextInputEditText 只读?

Kotlin Compose,在行中对齐元素

如何将 Kotlin 日期中的字符串或时间戳格式化为指定的首选格式?

如何为kotlin异常生成SerialVersionId?

在 Kotlin 中创建 Spinner 时,如何在 Fragment 中的旋转屏幕上修复指定为非空的参数为空?

Kotlin反射不可用