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"
}
For ordinal error:
//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())
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)
}
}
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() usage:
class PhotoGalleryAdapter(val ac : Activity, val result : ResponseNewsDetail) : PagerAdapter(){
override fun getCount(): Int = result.gallery!!.size()
.
.
}
Any ideas/suggestions would be appreciated. Cheers!
We have only declared, but not initialized the variable “users”. Error: (5, 9) Expression 'length' of type 'Int' cannot be invoked as a function. The function 'invoke ()' is not found
Regarding the error message: Kotlin was assuming an invoke operator, which you didn't supply. You may want to read what an invoke-operator is good for. Sometimes it comes in very handy. But for your problem the square brackets should suffice.
Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler). Variable number of arguments (Varargs)
If the value is greater than maximum kotlin.Long value (see built-in integer types ), it is an illegal integer literal and should be a compile-time error; Otherwise, if the value is greater than maximum kotlin.Int value (see built-in integer types ), it has type kotlin.Long;
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]
In Kotlin the expressions getter and setter differ from Java in the absence of parentheses.
getter: #Class.method
setter: #Class.method = value
e.g.
From: competitions.value(body?.competitionsList)
.
To: competitions.value = body?.competitionsList
e.g. 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
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With