android - ArrayOfNulls 与 Kotlin 的类型不匹配

标签 android arrays kotlin

我在将我的 java 代码转换为 kotlin 时遇到问题。

这行代码是我的问题所在

 dots = arrayOfNulls<TextView>(layouts.size)

它说:

Type mismatch. Required: Array(TextView)? - Found: Array(TextView?)

给你完整的代码,希望你能帮我找出问题所在。

class WelcomeActivity : AppCompatActivity() {

private var viewPager: ViewPager? = null
private var myViewPagerAdapter: MyViewPagerAdapter? = null
private var dotsLayout: LinearLayout? = null
private var dots: Array<TextView>? = null
private var layouts: IntArray? = null
private var btnSkip: Button? = null
private var btnNext: Button? = null
private var prefManager: PrefManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Checking for first time launch - before calling setContentView()
    prefManager = PrefManager(this)
    if (!prefManager!!.isFirstTimeLaunch) {
        launchHomeScreen()
        finish()
    }

    // Making notification bar transparent
    if (Build.VERSION.SDK_INT >= 21) {
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }

    setContentView(R.layout.welcomescreen)

    viewPager = findViewById(R.id.view_pager) as ViewPager
    dotsLayout = findViewById(R.id.layoutDots) as LinearLayout
    btnSkip = findViewById(R.id.btn_skip) as Button
    btnNext = findViewById(R.id.btn_next) as Button


    // layouts of all welcome sliders
    // add few more layouts if you want
    layouts = intArrayOf(R.layout.welcomescreen_slide1, R.layout.welcomescreen_slide2, R.layout.welcomescreen_slide3, R.layout.welcomescreen_slide4)

    // adding bottom dots
    addBottomDots(0)

    // making notification bar transparent
    changeStatusBarColor()

    myViewPagerAdapter = MyViewPagerAdapter()
    viewPager!!.adapter = myViewPagerAdapter
    viewPager!!.addOnPageChangeListener(viewPagerPageChangeListener)

    btnSkip!!.setOnClickListener { launchHomeScreen() }

    btnNext!!.setOnClickListener {
        // checking for last page
        // if last page home screen will be launched
        val current = getItem(+1)
        if (current < layouts!!.size) {
            // move to next screen
            viewPager!!.currentItem = current
        } else {
            launchHomeScreen()
        }
    }
}

private fun addBottomDots(currentPage: Int) {
    dots = arrayOfNulls<TextView>(layouts.size)

    val colorsActive = resources.getIntArray(R.array.array_dot_active)
    val colorsInactive = resources.getIntArray(R.array.array_dot_inactive)

    dotsLayout!!.removeAllViews()
    for (i in dots!!.indices) {
        dots[i] = TextView(this)
        dots!![i].text = Html.fromHtml("&#8226;")
        dots!![i].textSize = 35f
        dots!![i].setTextColor(colorsInactive[currentPage])
        dotsLayout!!.addView(dots!![i])
    }

    if (dots!!.size > 0)
        dots!![currentPage].setTextColor(colorsActive[currentPage])
}

提前致谢。

[编辑]:此外,我注意到此处的 for 语句中存在错误:

for (i in dots!!.indices) {
        dots[i] = TextView(this)
        dots!![i].text = Html.fromHtml("&#8226;")
        dots!![i].textSize = 35f
        dots!![i].setTextColor(colorsInactive[currentPage])
        dotsLayout!!.addView(dots!![i])
    }

首先,在第一行说Unresolve reference indices;然后在第二行说 dots[i] cannot smart cast to 'Array?'因为它是一个可变属性,最后在点之前的每个点后面的行中![i] 说“只有安全(?)或非空断言(!!.)调用才允许在类型为 TextView 的可为空的接收器上?

最佳答案

null 数组基本上是一个每个元素都等于 null 的数组.这意味着您的变量类型也必须接受空值。为此,您需要将变量类型更改为 Array<TextView?>? .

关于android - ArrayOfNulls 与 Kotlin 的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45553285/

相关文章:

android - 如何在 android studio 中将 2 列中的 6 个图像按钮与 3 行对齐?

java - 如何在java中进行碰撞检测

在所有其他应用程序之上运行的 Android 应用程序?

kotlin - 如何使用 JUnit 5 在 Kotlin 中创建 TestContainers 基础测试类

java - 在应用程序中间安装 apk - Android Studio

PHP - 我可以从特定键移动数组吗?

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

c++ - 数组排序,从高到低

kotlin - 为什么 Flux.flatMap() 不等待内部发布者完成?

android - NoSuchMethodError ConstantExpressionEvaluator.evaluateExpression 添加jetpack compose时