2021年8月29日日曜日

「Redundant SAM - constructor」になった時の対処法

 Android開発をKotlin言語でしていると、ボタン押下などのリスナー設定をしているときに
「Redundant SAM - constructor」が出ることがあるので、意味と対処法を残しておく。
まぁ、
カーソルを合わせたときに出る「Remove redundant SAM - constructor」を押すか、Alt+Shift+Enterキーの同時押しをすれば、意味を理解していなくても自動でコードを修正してくれますけどね..


2021年8月22日日曜日

Activityの遷移時、すでに生成しているActivityへ遷移する方法

startActivityでActivityを切り替える場合、
デフォルトのままだと、毎回新規でActivityのインスタンスを作ってしまう。
なので、すでに同名Activityが存在していても、新しいActivityがスタック上にできあがる。


Intentで指定したActivityがすでに生成されている場合は、
生成済みActivityに遷移させるにはどうするか。について書いていく。

2021年8月11日水曜日

Ktolinで「Parameter 'xxx' is never used」というワーニングが出たときの対応

KtolinでAndroid開発をしているとき、 
ListViewなど押下イベント設定(setOnItemClickListener)をラムダで書くと以下のようになる。
// 押下したリストのアイテム情報をトースト表示する
listView.setOnItemClickListener { parent, view, position, id ->
    Toast.makeText(this, array[position], Toast.LENGTH_SHORT).show()
}
こうすると、
パラメータ4つあるけど positionしか使っておらず、残りの引数に波線が表示される。


ワーニングメッセージを見るとそれぞれ
Parameter 'parent' is never used, could be renamed to _
Parameter 'view' is never used, could be renamed to _
Parameter 'id' is never used, could be renamed to _

となっている。

2021年8月8日日曜日

AndroidStudioでビルドしようとするとエラーでできない

Android Studio Arctic Foxにアップデートして、
新しいプロジェクト立ち上げてビルドすると以下のエラーになった。

Build file 'E:\Document\MyApplication11\app\build.gradle' line: 2

An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.


訳: 
Android Gradleプラグインを実行するには、Java11が必要です。
現在、Java1.8を使用しています。

つまり、
JDKのバージョンを11にすればよいということだ。