facebook twitter hatena line email

「Android/kotlin/javaからkotlinへの変換」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
("Assignments are not expressions, and only expressions are allowed in this context"エラーの時)
("Platform declaration clash: The following declarations have the same JVM signature"エラー)
行28: 行28:
 
=="Platform declaration clash: The following declarations have the same JVM signature"エラー==
 
=="Platform declaration clash: The following declarations have the same JVM signature"エラー==
 
publicプロパティはsetterが自動でつき、名前がかぶるため。
 
publicプロパティはsetterが自動でつき、名前がかぶるため。
 +
 
 
 
<pre>
 
<pre>

2020年2月17日 (月) 14:33時点における版

androidstudioでjavaからkotlinへの変換

  1. MainActivity.javaを開いた状態で、codeの"Convert Java File to Kotlin File"を選択
  2. MainActivity.javaがMainActivity.ktへ変更され、コードもkotlinとなる
  3. GradleにKotlinを認識させるために、Tool/Kotlin/ConfigureKotlinInProjectを選択

"Assignments are not expressions, and only expressions are allowed in this context"エラーの時

var bytesRead: Int
while ((bytesRead = dataInputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead)
}

var bytesRead: Int
do {
    bytesRead = dataInputStream.read(buffer)
    if (bytesRead == -1)
        break
    outputStream.write(buffer, 0, bytesRead)
} while (true)

参考:https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339

"Platform declaration clash: The following declarations have the same JVM signature"エラー

publicプロパティはsetterが自動でつき、名前がかぶるため。

class HogeClass {
    companion object {
        var name = ""
        fun setName(name: String) {
            this.name = name
        }
        fun getName(): String {
            return this.name
        }
    }
}

class HogeClass {
    companion object {
        private var name = ""
        fun setName(name: String) {
            this.name = name
        }
        fun getName(): String {
            return this.name
        }
    }
}

プロパティにprivateをつければ良い。