【问题标题】:Conflicting import, imported name 'Toast' is ambiguous导入冲突,导入名称“Toast”不明确
【发布时间】:2019-05-18 11:53:40
【问题描述】:

我正在尝试在 Circle CI 上运行构建,但在 android kotlin 中遇到了这个模棱两可的 checkstyle 错误,我不知道为什么会这样,下面是我遇到的错误:

/home/circleci/repo/app/src/main/java/com/andela/mrm/util/ConvergeUIUtils.kt: (12, 23): 导入冲突,导入名称 'Toast' 不明确 e: /home/circleci/repo/app/src/main/java/com/andela/mrm/util/ConvergeUIUtils.kt: (14, 23): 导入冲突,导入名称 'Toast' 不明确

下面是ConvergeUIUtils.kt 文件:

import android.content.Context
import android.graphics.Color
import android.graphics.PorterDuff
import android.support.constraint.ConstraintLayout
import android.support.design.widget.Snackbar
import android.support.v4.content.ContextCompat
import android.view.Gravity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.andela.mrm.R


/**
 * Important methods for the application UI.
 */
class ConvergeUIUtils
/**
 * Private constructor to prevent instantiation.
 */
private constructor() {
    init {
        throw UnsupportedOperationException()
    }

    companion object {
        /**
         * Show snackbar with edited properties.
         *
         * @param context the view context.
         * @param view the view to display the snackbar.
         * @param string the message to be displayed.
         */
        fun showSnackBar(context: Context, view: View, string: String) {
            val snackbar = Snackbar.make(view, string, Snackbar.LENGTH_LONG)
            val snackbarView = snackbar.view
            snackbarView.setPadding(10, 10, 10, 12)
            if (string.contains("extended")) {
                snackbarView.setBackgroundColor(ContextCompat.getColor(context, R.color.waiting_color))
            } else {
                snackbarView.setBackgroundColor(ContextCompat.getColor(context,
                        R.color.colorPrimaryDark))
            }
            snackbar.show()
        }

        /**
         * @param context the view context.
         * @param string the message of the toast.
         */

        fun showToast(context: Context, string: String) {
            val toast = Toast.makeText(context, string, Toast.LENGTH_LONG)
            toast.setGravity(Gravity.BOTTOM or Gravity.LEFT, 50, 50)
            val view = toast.view

            if (string.isNullOrBlank()) {

                view.setBackgroundResource(R.drawable.notification_success_img)
                toast.show()
            } else {

                if (string.contains("extended")) {
                    view.background.setColorFilter(ContextCompat.getColor(context, R.color.waiting_color),
                            PorterDuff.Mode.SRC_IN)
                    val text = view.findViewById<TextView>(android.R.id.message)
                    text.setTextColor(Color.WHITE)
                } else {
                    view.background.setColorFilter(ContextCompat.getColor(context,
                            R.color.colorPrimaryDark), PorterDuff.Mode.SRC_IN)
                }
                val text = view.findViewById<TextView>(android.R.id.message)
                text.setTextColor(Color.WHITE)
                toast.show()
            }

        }


        /**
         * set background color for free time button.
         *
         * @param context the view context.
         * @param freeDisplay the free time button.
         */
        fun setButtonColor(context: Context, freeDisplay: ConstraintLayout) {
            val normalDrawable = context.getDrawable(R.drawable.btn_room_extra_time)
            normalDrawable!!.setColorFilter(-0x1, PorterDuff.Mode.SRC_ATOP)
            freeDisplay.background = normalDrawable
        }

    }
}

我怎样才能克服这个 checkstyle 错误?

这是错误的图片。

【问题讨论】:

  • MCVE,好吗?
  • 您的导入有多个名为 Toast 的类。您必须使用 Toast 和包名,例如 android.widget.Toast.makeText()
  • @VladyslavMatviienko,我试过了,但它带来了同样的错误。

标签: java android kotlin checkstyle


【解决方案1】:

您的导入有多个名为 Toast 的类。

Kotlin 支持导入别名:https://kotlinlang.org/docs/reference/packages.html

import android.widget.Toast as WToast

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 2021-06-06
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2013-06-15
    • 2011-11-24
    相关资源
    最近更新 更多