我试图在旧的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)
}
}
长度():
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()
.
.
}
任何想法/建议将不胜感激。干杯!
答案 0 :(得分:19)
所有这些返回int的方法(String#length()
,...)都有一段时间以前成为属性。只需删除括号()
并以属性方式使用它。
var start = 0
var end = s.length //without ()
顺便说一句。 String
已有方法trim()
charAt
应替换为[]
运算符。因此,将s.charAt(end-1)
替换为s[end-1]
答案 1 :(得分:1)
在Kotlin中,getter和setter表达式与Java不同,因为没有括号。
获取器: #Class.method
设置者: #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