我正在尝试解决一个旧的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
Run Code Online (Sandbox Code Playgroud)
我的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"
}
Run Code Online (Sandbox Code Playgroud)
对于序数错误:
//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())
Run Code Online (Sandbox Code Playgroud)
对于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)
}
}
Run Code Online (Sandbox Code Playgroud)
长度():
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)
}
}
Run Code Online (Sandbox Code Playgroud)
size()用法:
class PhotoGalleryAdapter(val ac : Activity, val result : ResponseNewsDetail) : PagerAdapter(){
override fun getCount(): Int = result.gallery!!.size()
.
.
}
Run Code Online (Sandbox Code Playgroud)
任何想法/建议将不胜感激.干杯!
小智 25
所有那些返回int的方法(String#length(),...)都有一段时间以前成为属性.只需删除括号()并以属性方式使用它.
var start = 0
var end = s.length //without ()
Run Code Online (Sandbox Code Playgroud)
顺便说一句.String已经有了方法trim()
charAt应该由[]操作员替换.因此,更换s.charAt(end-1)用s[end-1]
COR*_*ian 13
在 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10715 次 |
| 最近记录: |