Kulwinder Singh
05/15/2019, 5:08 AMsortedBy
function is throwing Null pointer even if my List type is not nullablegildor
05/15/2019, 5:12 AMKulwinder Singh
05/15/2019, 5:18 AMgildor
05/15/2019, 5:19 AMgildor
05/15/2019, 5:20 AMKulwinder Singh
05/15/2019, 5:29 AMFatal Exception: kotlin.KotlinNullPointerException
at FragmentName$sortByCompleteness$$inlined$sortedBy$1.compare(Comparisons.kt:320)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
at java.util.TimSort.sort(TimSort.java:220)
at java.util.Arrays.sort(Arrays.java:1432)
at kotlin.collections.ArraysKt___ArraysJvmKt.sortWith(_ArraysJvm.kt:1737)
at kotlin.collections.CollectionsKt___CollectionsKt.sortedWith(_Collections.kt:926)
at FragmentName.sortByCompleteness(FragmentName.kt:224)
at FragmentName.loadNativeAds(FragmentName.kt:95)
at FragmentName$loadNativeAds$1.invokeSuspend(Unknown Source:12)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:285)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
gildor
05/15/2019, 5:29 AMcontext
is nullKulwinder Singh
05/15/2019, 5:29 AMcontext
?gildor
05/15/2019, 5:29 AMgildor
05/15/2019, 5:29 AMgildor
05/15/2019, 5:30 AMgildor
05/15/2019, 5:30 AMgildor
05/15/2019, 5:31 AM!!
gildor
05/15/2019, 5:31 AMgildor
05/15/2019, 5:32 AMcontext!!
with requireContext() you will get much better error report, like:
Fragment FragmentName not attached to a context
Kulwinder Singh
05/15/2019, 5:38 AMKulwinder Singh
05/15/2019, 5:44 AMcontext!!
in Prefs.getPercentageOfPractice
Kulwinder Singh
05/15/2019, 5:45 AMgildor
05/15/2019, 5:45 AMKulwinder Singh
05/15/2019, 5:52 AMuiScope.launch{
loadListItems()?.let { loadNativeAds(it) }
}
private suspend fun loadNativeAds(categories: List<Category2>) {
val list: List<Any> = try {
nativeAdsManager.loadAdsSuspended()
nativeAdsManager.fillAdsInList(list = categories, isItAdPosition = ::isAdPosition)
} catch (e: IllegalStateException) {
categories
}
displayCategories(list.sortByCompleteness())
}
private fun List<Any>.sortByCompleteness(): List<Any> {
return sortedBy { it is Category2 && Prefs.getPercentageOfPractice(context!!, it) == 100 }//i will change this to requireContext()
}
Kulwinder Singh
05/15/2019, 5:52 AMgildor
05/15/2019, 5:53 AMuiScope.launch{
? How do you create it?Kulwinder Singh
05/15/2019, 5:54 AMuiScope
is field of fragment created using private val uiScope by lazy { LifecycleScope(lifecycle) }
gildor
05/15/2019, 5:57 AMgildor
05/15/2019, 5:57 AMlifecycleOwner.lifecycleScope.launch
directlyKulwinder Singh
05/15/2019, 5:57 AMclass LifecycleScope(lifecycle: Lifecycle) : DefaultLifecycleObserver, CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext = job + Dispatchers.Main
init {
lifecycle.addObserver(this)
}
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
job.cancel()
}
}
gildor
05/15/2019, 5:59 AMgildor
05/15/2019, 6:00 AMKulwinder Singh
05/15/2019, 6:03 AMgildor
05/15/2019, 6:03 AMgildor
05/15/2019, 6:04 AMgildor
05/15/2019, 6:04 AMgildor
05/15/2019, 6:04 AMKulwinder Singh
05/15/2019, 6:30 AM'androidx.lifecycle:lifecycle-extensions-ktx:2.0.0'
but it not working ?, please give send me any documentation page if you know?gildor
05/15/2019, 6:34 AMgildor
05/15/2019, 6:34 AMKulwinder Singh
05/15/2019, 7:21 AMKulwinder Singh
05/15/2019, 7:21 AMKulwinder Singh
05/15/2019, 7:29 AMonStop
is called, so can you please tell me do it going to work as i'm expecting?, also i have converted uiScope
as local variable
val uiScope = LifecycleScope(lifecycle, cancelChildrenOnStop=false)
uiScope.launch{
loadListItems()?.let { loadNativeAds(it) }
}
class LifecycleScope(
lifecycle: Lifecycle,
private val cancelChildrenOnStop: Boolean = false
) : DefaultLifecycleObserver, CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext = job + Dispatchers.Main
init {
lifecycle.addObserver(this)
}
//added this method
override fun onStop(owner: LifecycleOwner) {
super.onStop(owner)
if (cancelChildrenOnStop)
job.cancelChildren()
}
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
job.cancel()
}
}
gildor
05/15/2019, 7:31 AMgildor
05/15/2019, 7:32 AMgildor
05/15/2019, 7:32 AMKulwinder Singh
05/15/2019, 8:19 AMgildor
05/15/2019, 8:20 AM