「Android/kotlin/javaからkotlinへの変換」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→"Assignments are not expressions, and only expressions are allowed in this context"エラーの時) |
(→"Assignments are not expressions, and only expressions are allowed in this context"エラーの時) |
||
| 行25: | 行25: | ||
参考:https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339 | 参考: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が自動でつき、名前がかぶるため。 | ||
| + | 前 | ||
| + | <pre> | ||
| + | class HogeClass { | ||
| + | companion object { | ||
| + | var name = "" | ||
| + | fun setName(name: String) { | ||
| + | this.name = name | ||
| + | } | ||
| + | fun getName(): String { | ||
| + | return this.name | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | 後 | ||
| + | <pre> | ||
| + | class HogeClass { | ||
| + | companion object { | ||
| + | private var name = "" | ||
| + | fun setName(name: String) { | ||
| + | this.name = name | ||
| + | } | ||
| + | fun getName(): String { | ||
| + | return this.name | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | プロパティにprivateをつければ良い。 | ||
2020年2月17日 (月) 14:32時点における版
androidstudioでjavaからkotlinへの変換
- MainActivity.javaを開いた状態で、codeの"Convert Java File to Kotlin File"を選択
- MainActivity.javaがMainActivity.ktへ変更され、コードもkotlinとなる
- 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をつければ良い。
