Tag: kotlin android extensions

Android Kotlin – Volley意外的响应代码400

我使用Django制作了REST API,现在我想将它连接到android。 我正在使用Kotlin和Volley库。 我创建的代码,我仍然得到这个错误: E / Volley:BasicNetwork.performRequest:用于http://laude.ct8.pl/api/user/login/的意外响应代码400。 这是我的Kotlin代码: val jsonObj = JSONObject() val LOGIN_API_URL = “http://laude.ct8.pl/api/user/login/” loginBtn.setOnClickListener { jsonObj.put(“username”, username.text) jsonObj.put(“password”, passwd.text) val que = Volley.newRequestQueue(this@MainActivity) val req = JsonObjectRequest(Request.Method.POST, LOGIN_API_URL, Response.Listener { response -> Toast.makeText(this@MainActivity, response.toString(), Toast.LENGTH_LONG).show() }, Response.ErrorListener { error -> Toast.makeText(this@MainActivity, error.toString(), Toast.LENGTH_LONG).show() }) que.add(req) } 我用Content-Type看到了这个问题,所以我尝试了这一行,并将其添加到jsonObj.put(“password”, passwd.text) 。 这里是这一行: jsonObj.put(“Content-Type”, “application/json”) […]

我无法理解kotlin中string.kt的源代码实现

在kotlin源代码中,我无法理解如何实现String.kt的长度,如下所示: package kotlin public class String : Comparable, CharSequence { companion object {} /** * Returns a string obtained by concatenating this string with the string representation of the given [other] object. */ public operator fun plus(other: Any?): String public override val length: Int public override fun get(index: Int): Char public override fun subSequence(startIndex: Int, […]

Kotlin DialogFragment editText可编辑始终为空

所以,我使用的Kotlin扩展很简单,但是我无法从edittext获取字符串 这里是我的代码: override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val v = activity.layoutInflater .inflate(R.layout.dialog_group, null) v.add_group_button.setOnClickListener(addListener) return AlertDialog.Builder(activity) .setView(v) .create() } private var addListener: View.OnClickListener = View.OnClickListener { val groupNameInput: String = view?.group_edit_text?.text.toString() } 当我按下添加按钮groupNameInput总是返回null为什么?

Kotlin数组创建:参数“init”没有值传递

我正在Android Studio中将Java文件转换为Kotlin,并且出现此错误: 参数“init”没有传递值 我通过添加lateinit稍微修改了代码 java代码是: private TextView[] dots; private int[] layouts; private void addBottomDots(int currentPage) { dots = new TextView[layouts.length]; //some lines here } 而相应的Kotlin代码是 private lateinit var dots: Array private lateinit var layouts: IntArray private fun addBottomDots(currentPage: Int) { dots = Array(layouts.size) // error happens here // some lines here } 因为我是Kotlin的新手,我不明白为什么这是原因

没有大括号的Kotlin类

我注意到,我们可以在Kotlin中创建类,而不需要下面的大括号。 //Example classFile.kt class Empty class SecondEmpty fun firstMethod() { } 我的问题是,为什么我们需要这样的function? 在哪种情况下我们可以用这个? 在上面的例子中,我写了一个名为firstMethod()的方法,我怎样才能从其他对象调用?

如何在Android上使用Kotlin在BottomNavigationView上设置OnNavigationItemListener?

我使用kotlin-android-extension,我可以从layout文件调用bottomNavigationView id到kotlin文件。 我可以使用bottomNavigationView.setOnNavigationItemSelectedListener(BottomNavigationView.OnNavigationItemSelectedListener {}) ,但接下来呢? 据我所知在Java中,有另一个名为onNavigationItemSelected函数,但我无法在kotlin中find它。 这是我想在Java中使用的示例代码,但不能写在kotlin中。 bottomNavigationView.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.action_favorites: case R.id.action_schedules: case R.id.action_music: } return true; } });

和…之间的不同 !! 和? 在Kotlin

我是Kotlin新手。 我想知道这两个之间的区别!! 和? 在下面的代码。 我有以下两个片段首先使用!! 为mCurrentDataset和另一个有? 对于相同的variables。 if(!mCurrentDataset!!.load(mDataSetString.get(mCurrentDataSelectionIndex), STORAGE_TYPE.STORAGE_APPRESOURCE)) { Log.d(“MyActivity”,”Failed to load data.”) return false } if(!mCurrentDataset?.load(mDataSetString.get(mCurrentDataSelectionIndex), STORAGE_TYPE.STORAGE_APPRESOURCE)!!) { Log.d(“MyActivity”,”Failed to load data.”) return false } 提前致谢。

科特林向我解释了“背景场”

我一直在看Kotlin官方教程。 我遇到了一个名为Backing Fields的主题 它说, Kotlin的课程不能有字段。 但是,使用自定义访问器时有时需要有后台字段。 为了这些目的,Kotlin提供了一个可以使用字段标识符访问的自动后台字段: var counter = 0 // the initializer value is written directly to the backing field set(value) { if (value >= 0) field = value } 我从这个官方链接得到了上述 我的问题是,是“领域”指向计数器variables? 有人能给我提供一个支持领域的例子,或者用一个理解的词来形容我吗?

Kotlin Android扩展无法解析“文本”

所以我试图摆脱“findViewById”,并决定添加Kotlin Android扩展到我的项目 在我的gradle里面: apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ apply plugin: ‘kotlin-android-extensions’ 在项目级别gradle.build中: classpath “org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version” 在我的MainActivity中: import kotlinx.android.synthetic.main.activity_main.* 不过,我仍然需要var editTextCity = editText_city.findViewById(R.id.editText_city) 解决var city = editText_city.text.toString() 看起来像扩展不工作

Java和Kotlin组合生成错误(重复邮编条目)

我已经将Kotlin库添加到了我现有的项目中。 之后,我得到构建错误。 我评论了最近添加的所有库,并检查了添加kotlin库后的主要问题 Error:Execution failed for task ‘:app:transformClassesWithMultidexlistForDebug’. > java.io.IOException: Can’t write [/home/imedrix-server/StudioProjects/kardioscreen-operatorapp/app/build/intermediates/multi-dex/debug/componentClasses.jar] (Can’t read [/home/imedrix-server/StudioProjects/kardioscreen-operatorapp/app/build/intermediates/transforms/desugar/debug/76.jar(;;;;;;**.class)] (Duplicate zip entry [76.jar:org/intellij/lang/annotations/Flow.class])) 项目gradle buildscript { ext.kotlin_version = ‘1.2.21’ repositories { google() jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:3.0.1’ classpath ‘com.google.gms:google-services:3.0.0’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” classpath (‘com.google.firebase:firebase-plugins:1.0.5’) { exclude group: ‘com.google.guava’, module: ‘guava-jdk5’ } } } allprojects { repositories { google() […]