3
votes

I got a problem when converting my java code to kotlin.

This line of code is what making me the problem

 dots = arrayOfNulls<TextView>(layouts.size)

it said:

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

Let you here the whole code and hope you can give me hand to figure out what's wrong.

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])
}

Thanks in advance.

[EDIT]: Also, I noticed an error in a for statement, here:

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])
    }

First, in the first line said Unresolve reference indices; then in second line say that dots[i] cannot smart cast to 'Array?' because it's a mutable property and finally in the lines behind every dot before dots!![i] says "Only safe (?) or not-null asserted (!!.) calls are allowed on a nullable receiver of type TextView?

2

2 Answers

7
votes

An array of nulls is basically an array with every element equal to null. That means that your variable's type must accept nulls as well. To accomplish this you need to change your variable type to Array<TextView?>?.

1
votes

Use:

private lateinit var dots: Array<TextView?>

Instead of:

private var dots: Array<TextView>? = null