【问题标题】:I am modifying Java with Kotlin, but I am struggling to fix the wrong parts(edit )我正在使用 Kotlin 修改 Java,但我正在努力修复错误的部分(编辑)
【发布时间】:2018-11-14 15:31:58
【问题描述】:

如果你能帮助我,我将不胜感激。

activity.kotlin

var postsMap = HashMap<>() //Here error
postsMap.put("uid", current_user_id)
postsMap.put("date", saveCurrentDate)
postsMap.put("time", saveCurrentTime)
postsMap.put("description", Description)
postsMap.put("postimage", downloadUrl)
postsMap.put("profileimage", userProfileImage)
postsMap.put("fullname", userFullName)
PostsRef.child(current_user_id + postRandomName).updateChildren(postsMap)//Here error
    .addOnCompleteListener(object : OnCompleteListener {//Here error
        override fun onComplete(task: Task<*>) { //Here error
            if (task.isSuccessful) {
                SendUserToMainActivity()
                Toast.makeText(
                    this@PostActivity,
                    "New Post is updated successfully.",
                    Toast.LENGTH_SHORT
                ).show()

            } else {
                Toast.makeText(
                    this@PostActivity,
                    "Error Occured while updating your post.",
                    Toast.LENGTH_SHORT
                ).show()

            }
        }
    })

抱歉,问题不准确。 谢谢

我通过回答更正了代码。谢谢。

val postsMap: HashMap = hashMapOf()

PostsRef!!.child(current_user_id + postRandomName).updateChildren(postsMap)

【问题讨论】:

  • 你的 IDE 告诉你关于代码下划线部分的什么?
  • 能把java代码也放上来吗?
  • 因为它应该是val postsMap: HashMap&lt;String, Any?&gt; = hashMapOf()
  • 您是否尝试过用 Java 编写代码并让 Android Studio(我假设您正在使用 Android Studio)为您转换代码并查看它如何转换以了解发生了什么问题?
  • 哦,谢谢我已经解决了这个问题。

标签: java android kotlin


【解决方案1】:

HashMap 需要类型参数。将第一行更改为

val postsMap = HashMap<String, String>()

val postsMap = HashMap<String, Any?>()

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 2022-08-14
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 2018-11-30
    相关资源
    最近更新 更多