kotlin

Kotlin smart cast with nullable variable

允我心安 提交于 2020-06-27 14:08:39
问题 When I'm trying to create the following code: class SmartCast { var array: MutableList<Int>? = null fun processArray() { if (array != null && !array.isEmpty()) { // process } } } this error is shown: Smart cast to 'MutableList' is impossible, because 'array' is a mutable property that could have been changed by this time It's clear that the array variable can be changed to null in case of multi-threading. But if I use @Synchronized annotation, there is no way to mutate the variable in between

Kotlin smart cast with nullable variable

∥☆過路亽.° 提交于 2020-06-27 14:07:23
问题 When I'm trying to create the following code: class SmartCast { var array: MutableList<Int>? = null fun processArray() { if (array != null && !array.isEmpty()) { // process } } } this error is shown: Smart cast to 'MutableList' is impossible, because 'array' is a mutable property that could have been changed by this time It's clear that the array variable can be changed to null in case of multi-threading. But if I use @Synchronized annotation, there is no way to mutate the variable in between

Kotlin smart cast with nullable variable

≯℡__Kan透↙ 提交于 2020-06-27 14:07:08
问题 When I'm trying to create the following code: class SmartCast { var array: MutableList<Int>? = null fun processArray() { if (array != null && !array.isEmpty()) { // process } } } this error is shown: Smart cast to 'MutableList' is impossible, because 'array' is a mutable property that could have been changed by this time It's clear that the array variable can be changed to null in case of multi-threading. But if I use @Synchronized annotation, there is no way to mutate the variable in between

Scrolling behaviour conflicts with a child RecyclerView and a parent Viewpager2

六眼飞鱼酱① 提交于 2020-06-27 12:54:05
问题 I have a vertical scrolling ViewPager2 and the last children contains a RecyclerView scrolling on the same direction. This is causing a conflicting behaviour, the ViewPager2 always steal the scroll event when I am at the page containing this RecyclerView . The only way to make the scroll inside the RecyclerView is if I scroll really slow, if I make it fast, like a swipe event the ViewPager2 gets scrolled and changes the page. Currently I'm doing a fix that involves disabled the user

Kotlin: coroutineScope is slower than GlobalScope

不羁岁月 提交于 2020-06-27 12:51:48
问题 I'm learning coroutines, and I encounter the following surprising (for me) behavior. I want to have a parallel map. I consider 4 solutions: Just map , no parallelism pmap from here. Modification of item 2: I removed coroutineScope and use GlobalScope . Java's parallelStream . The code: import kotlinx.coroutines.* import kotlin.streams.toList import kotlin.system.measureNanoTime inline fun printTime(msg: String, f: () -> Unit) = println("${msg.padEnd(15)} time: ${measureNanoTime(f) / 1e9}")

BuildConfig is public, should be declared in a file named BuildConfig.java

瘦欲@ 提交于 2020-06-27 12:50:34
问题 Seem to be getting this occurring error whenever I run my android application. The only way to get around it is by rebuilding the project and runs normally again. If anyone knows the reason for this will be much appreciated 回答1: Fixed Update: Cloned a fresh copy of my code and deleted some of the irrelevant files not needed here What should be in my .gitignore for an Android Studio project? 来源: https://stackoverflow.com/questions/59438157/buildconfig-is-public-should-be-declared-in-a-file

Building Kotlin projects with Gradle sometimes fails with NoClassDefFoundError

*爱你&永不变心* 提交于 2020-06-27 12:24:29
问题 I can't reliably reproduce this problem because it is sporadic. This is what I get when I build a Kotlin project with Gradle : Could not perform incremental compilation: Could not connect to Kotlin compile daemon Could not connect to kotlin daemon. Using fallback strategy. :myproj:compileKotlin FAILED 1 actionable task: 1 executed e: java.lang.NoClassDefFoundError: Could not initialize class kotlin.Unit warning: the '-d' option with a directory destination is ignored because '-Xbuild-file' is

Eye indicator overlaps Hint error icon in AutoCompleteTextView

五迷三道 提交于 2020-06-27 11:55:28
问题 Code: <android.support.design.widget.TextInputLayout android:id="@+id/textInputLayout" android:layout_width="match_parent" android:layout_height="52dp" android:background="@android:color/white" android:hint="@string/current_password" android:paddingEnd="14dp" android:paddingLeft="14dp" android:paddingRight="14dp" android:paddingStart="14dp" android:paddingTop="4dp" android:scrollbarAlwaysDrawHorizontalTrack="true" android:textColorHint="@color/darkBlue" app:layout_constraintTop_toBottomOf="@

Run two Kotlin coroutines inside coroutine in parallel

ぃ、小莉子 提交于 2020-06-27 09:10:08
问题 I have two suspend functions: suspend fun sendData() : Boolean suspend fun awaitAcknowledge() : Boolean and I want to wrap them in a third suspend function in which they should be executed in parallel and I want to calculate the final result by having both return values: suspend fun sendDataAndAwaitAcknowledge() : Boolean { // TODO execute both in parallel and compare both results } However, if I write it like that, suspend fun sendDataAndAwaitAcknowledge() : Boolean { val sendResult =

Mapping arrays to list of objects kotlin

你说的曾经没有我的故事 提交于 2020-06-27 08:27:29
问题 I'm wondering about methods of mapping multiple arrays into one list of object. I mean e.g. I have val a = arrayOf("A1","A2","A3") val b = arrayOf("B1","B2","B3") and data class SomeClass(val v1:String, val v2:String) I want to parse it in elegant way to have list like that: val list = listOf(SomeClass("A1","B1"),SomeClass("A2","B2"),SomeClass("A3","B3")) I assume they are of the same length. The only way I thought of is: val list = mutableListOf<SomeClass>() for (i in a.indices) array.add