How to add an item to a list in Kotlin?

A more idiomatic approach would be to use MutableList instead of specifically ArrayList. You can declare: val listOfVehicleNames: MutableList<String> = mutableListOf() And add to it that way. Alternatively, you may wish to prefer immutability, and declare it as: var listOfVehicleNames: List<String> = emptyList() And in your completion block, simply reassign it: listOfVehicleNames = response.body()?.message()?.orEmpty() .map … Read more

Accidental override: The following declarations have the same JVM signature

This happens because the Kotlin compiler tries to generate a getter for val context declared in your class primary constructor, namely a method getContext(), but the base class ArrayAdapter<T> already has such a method. You can solve that by doing one of the following: Change your class’ constructor parameter not to be a val. class … Read more

ViewBinding vs Kotlin Android Extensions with synthetic views

Let’s review the two. Configuration Kotlin Android Extensions Import appropriate layout synthetic extensions: import kotlinx.android.synthetic.main.<layout>.* Reference views in code via their ids: textView.text = “Hello, world!”. These extensions work on: Activities, Fragments and Views. View Binding Create binding reference inside your class: private lateinit var binding YourClassBinding Inflate your binding binding = YourClassBinding.inflate(layoutInflater) inside Activity‘s … Read more

how to get viewModel by viewModels? (fragment-ktx)

Atlast we we have got stable version. After moving to implementation ‘androidx.fragment:fragment-ktx:1.1.0’ i faced another issue. ###Compiler Error: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 ###build.gradle (Module:app) compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 } kotlinOptions { jvmTarget = “1.8” } reference After … Read more

Unresolved reference: kotlinx

Add kotlin-android-extensions in our buildscript’s dependencies: 1. In your project-level build.gradle buildscript { dependencies { classpath “org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version” } } and apply the kotlin-android-extensions plugin: 2. In your module-level build.gradle apply plugin: ‘kotlin-android-extensions’

Outdated Kotlin Runtime warning in Android Studio

In your (Project: [projectName]) build.gradle file find this: ext.kotlin_version = ‘x.x.x’ and replace x.x.x with the current version of your Kotlin plugin. In order to check which is the current version of your Kotlin plugin: Go to: Tools -> Kotlin -> Confugure Kotlin Plugin Updates Click “Check again”. After a second you will see the … Read more

NullPointerException when trying to access views in a Kotlin fragment

Kotlin synthetic properties are not magic and work in a very simple way. When you access btn_K, it calls for getView().findViewById(R.id.btn_K). The problem is that you are accessing it too soon. getView() returns null in onCreateView. Try doing it in the onViewCreated method: override fun onViewCreated(view: View, savedInstanceState: Bundle?) { btn_K.setOnClickListener { Log.d(TAG, “onViewCreated(): hello … Read more