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

这行代码是我遇到问题的原因

 dots = arrayOfNulls<TextView>(layouts.size)

它说:



让你把整个代码放在这里,希望你能帮我找出问题所在。
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 引用索引 ;然后在第二行说 dots[i] 不能智能转换为 'Array?'因为它是一个可变属性,最后在点之前的每个点后面的行中!![i] 说“在 TextView 类型的可空接收器上只允许安全 (?) 或非空断言 (!!.) 调用?

最佳答案

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

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

10-14 23:45