kotlin

Kotlin Coroutines suspend fun + retrofit throws “No Retrofit annotation found” error

你。 提交于 2020-07-15 15:16:11
问题 I'm trying to use retrofit's coroutine support in 2.5.1-SNAPSHOT but I keep getting a strange exception. My retrofit service class has: @GET("weather") suspend fun getForecast(@Query("q") query: String, @Query("num_of_days") numDays: String = "1", @Query("format") format : String = "json", @Query("key") apiKey: String = API_KEY) : Weather And when I try to call it I get: 2019-05-18 13:57:01.507 27422-27477/com.my.app E/MainPresenter$onResume$$inlined$CoroutineExceptionHandler: Something went

Kotlin Coroutines suspend fun + retrofit throws “No Retrofit annotation found” error

拈花ヽ惹草 提交于 2020-07-15 15:16:07
问题 I'm trying to use retrofit's coroutine support in 2.5.1-SNAPSHOT but I keep getting a strange exception. My retrofit service class has: @GET("weather") suspend fun getForecast(@Query("q") query: String, @Query("num_of_days") numDays: String = "1", @Query("format") format : String = "json", @Query("key") apiKey: String = API_KEY) : Weather And when I try to call it I get: 2019-05-18 13:57:01.507 27422-27477/com.my.app E/MainPresenter$onResume$$inlined$CoroutineExceptionHandler: Something went

What are underscore names in Kotlin reserved for?

时光怂恿深爱的人放手 提交于 2020-07-15 09:26:17
问题 In Python you can use _ as a variable name. If I write e.g. val _ = 3 in Kotlin IntelliJ gives me an error with: Names _, __, ___, ..., are reserved in Kotlin What are they reserved for? What is their function? 回答1: The single underscore is already used in several ways where you want to skip a parameter or a component and don't want to give it a name: For ignoring parameters in lambda expressions: val l = listOf(1, 2, 3) l.forEachIndexed { index, _ -> println(index) } For unused components in

Cannot get Switch menu item because R.id.* is different from menu items' internal ids

半城伤御伤魂 提交于 2020-07-10 10:28:38
问题 I am not able to use setOnCheckedChangeListener because getting a Switch menu item by id won't work as the resource id is different from the menu id. This is what I've got: Toolbar <?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:theme="@style/Theme.AppCompat.Light.DarkActionBar"

How to search for a String in Android's Strings.xml?

拥有回忆 提交于 2020-07-10 10:27:25
问题 I tried this answer: How to search content of strings stored in XML file?: companion object { val TAG: String = "EffectsDescription" fun fromStringsXml(string: String, context: Context): String { val ss = string.toLowerCase().replace(" ", "_"); Log.d(TAG, "gonna search for " + ss); val s = searchForString(ss, context); Log.d(TAG, "found: " + s); return s ?: "ERROR" } private fun searchForString(message: String, context: Context): String? { return try { val resId: Int = context.resources

Cannot make a generic item in Kotlin

浪尽此生 提交于 2020-07-10 10:26:45
问题 I want to make the following line of code generic: val newItem: Item = documentChange.document.toObject<Item>(Item::class.java) Works fine. However, when I try to make it generic: val addedItem: T = documentChange.document.toObject<T>(T::class.java) Android Studio is complaining: Cannot use 'T' as reified type parameter. Use a class instead. Screenshot How can I make it generic? 回答1: You need to make a function inline and add reified keyword to the generic parameter: inline fun <reified T>

How to use 'I' as reified type parameter in a LiveData<I> class?

旧城冷巷雨未停 提交于 2020-07-10 10:26:38
问题 I'm trying to use generics when subclassing a LiveData<I> class . According to this answer, I have tried this: class ItemLiveData<I>(): LiveData<I>() { override fun onEvent(querySnapshot: QuerySnapshot?, e: FirebaseFirestoreException?) { if (e != null) return for (documentChange in querySnapshot!!.documentChanges) { when (documentChange.type) { DocumentChange.Type.ADDED -> setValue(getItem(documentChange)) //Add to LiveData } } private inline fun <reified I> getItem(doc: DocumentChange) = doc

How do I correctly register a plugin with the FCM plugin

雨燕双飞 提交于 2020-07-10 10:26:27
问题 I am using https://pub.dev/packages/firebase_messaging v6.0.16 and flutter v1.17.5 And I am trying to call a plugin inside my myBackgroundMessageHandler on the line where it says AppAvailability.launchApp('com.companyname.appname'); class CloudMessagingService extends NotificationService { final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); void configure() { if (Platform.isIOS) _firebaseMessaging.requestNotificationPermissions(IosNotificationSettings()); _firebaseMessaging