kotlin-android-extensions

Function in Kotlin data class as argument leads to parceling error

穿精又带淫゛_ 提交于 2019-12-08 21:56:25
问题 I have a data class in Kotlin hat is using the @Parcelize annotation for easy parcelization. Thing is I now want to pass a function to this class and I do not really know how to make the function not be considered during parceling. This is my data class: @Parcelize data class GearCategoryViewModel( val title: String, val imageUrl: String, val categoryId: Int, val comingSoon: Boolean, @IgnoredOnParcel val onClick: (gearCategoryViewModel: GearCategoryViewModel) -> Unit ) : DataBindingAdapter

Programmatically inflated layout with Kotlin Android Extensions

非 Y 不嫁゛ 提交于 2019-12-08 14:58:18
问题 I have a following layout: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/white" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:id="@+id/tvErrorTitle" android:layout_width="wrap_content" android:layout_height="wrap

How to properly use the URL with Kotlin Android

笑着哭i 提交于 2019-12-08 08:41:55
问题 I want to use override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val json = URL("https://my-api-url.com/something").readText() simpleTextView.setText(json) } but this fatal error occurs FATAL EXCEPTION: main Process: com.mypackage.randompackage, PID: 812 java.lang.RuntimeException: Unable to start activity ComponentInfo{ ***.MainActivity}: android.os.NetworkOnMainThreadException How can I simply read a JSON from a URL

Dynamically create controls in android using Kotlin

蹲街弑〆低调 提交于 2019-12-08 06:42:37
问题 Using java, to create a control dynamically we use something like TextView textview=new TextView(getApplicationContext()); how can the same be done in Kotlin? var textview = TextView does't work, nor does var textview as TextView unfortunately, haven't even encountered any good kotlin tutorials for android. update-Actually am trying to create dynamic listview with a custom layout. 回答1: You can, by calling the constructor of TextView , like so: var textview = TextView(this) // "this" being the

Kotlin Android App compilation failed with message: Unresolved reference: kotlinx

折月煮酒 提交于 2019-12-07 04:37:51
问题 I am testing out a basic Kotlin-based Android app based on the instructions in the "Kotlin for Android Developers" book. I am using Android Studio 2.1.1. I have the following build.grade (Project: WeatherApp) setup: allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } I have the following build.grade (Module:App) setup: apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' buildscript {

Referencing views with the same id in different layouts with kotlin android extensions

走远了吗. 提交于 2019-12-06 23:23:24
问题 In my Android project I have two layouts: num_info and num_info_pack. Both have views with id "circle". So I thought referencing those views by layout_name.circle would solve the problem: val inetView = activity.layoutInflater.inflate(R.layout.num_info_pack, parent, false) inetView.circle.setBackgroundResource(background) But circle is underlined with red and it says: Overload resolution ambiguity. All these functions match. public val View.circle: View! defined in kotlinx.android.synthetic

Extremely slow in autocompletion & code analysis for Kotlin projects in Intellij IDEA

我是研究僧i 提交于 2019-12-06 23:15:35
问题 We have a project on IDEA that consists of a couple med sized Java packages and one very small Kotlin package (5 files). I noticed performance is fine with any Java packages, but it's 10x slower in autocompletion, code analysis and compilation for the very small Kotlin package. Autocompletion occasionally was so slow to a point where the popover couldn't load all the methods and it had to load a couple API incrementally. Every time our developer types a word and wait for autocomplete, it

How to properly use the URL with Kotlin Android

梦想与她 提交于 2019-12-06 16:39:36
I want to use override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val json = URL("https://my-api-url.com/something").readText() simpleTextView.setText(json) } but this fatal error occurs FATAL EXCEPTION: main Process: com.mypackage.randompackage, PID: 812 java.lang.RuntimeException: Unable to start activity ComponentInfo{ ***.MainActivity}: android.os.NetworkOnMainThreadException How can I simply read a JSON from a URL link? The package of the async function doesn't exist. Android doesn't allow accessing the internet

Kotlin View Binding java.lang.IllegalStateException: view must not be null inside listener

心已入冬 提交于 2019-12-06 11:14:17
问题 I'm using View Binding from Kotlin Android Extensions: import kotlinx.android.synthetic.main.fragment_user_profile.* I want to display a value from Cloud Firestore in a fragment : FirebaseFirestore.getInstance() .collection("users") .document("1") .get() .addOnSuccessListener { doc -> my_text_view.text = doc["name"] as String } It works if the fragment is still shown when data is received. But if user close the fragment (pressing back) before data is received, it crashes: java.lang

How can i access a char in string in at specific number? [closed]

旧城冷巷雨未停 提交于 2019-12-05 15:13:24
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I tried this code but it is giving me errors. So how can i access a character in a string in kotlin. in java it can be done by charAt() method. private fun abc(x: String) { var i: Int = 0 while (x[i].toString() != "+") { var y: Char = x[i] i++ } } The equivalent of Javas String.charAt() in Kotlin is String.get() . Since this is implemented as an operator, you can use [index] instead of get(index) . For example val