繁体   English   中英

需要从Java到Kotlin正确转换的建议

[英]Needed suggestion for proper conversion from Java to Kotlin

Android Studio具有自动的Java到Kotlin转换器。 在某些情况下,它不能顺利运行,这意味着需要一些手动工作。

这是一个例子:

private TextView[] dots;

 private void addBottomDots(int currentPage) {
        dots = new TextView[layouts.length];

        int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
        int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);

        dotsLayout.removeAllViews();
        for (int i = 0; i < dots.length; i++) {
            dots[i] = new TextView(getContext());
            dots[i].setText(Html.fromHtml("&#8226;"));
            dots[i].setTextSize(35);
            dots[i].setTextColor(colorsInactive[currentPage]);
            dotsLayout.addView(dots[i]);
        }

        if (dots.length > 0)
            dots[currentPage].setTextColor(colorsActive[currentPage]);
    }

自动转换+手动调整的结果是:

private var dots: Array<TextView?>? = null  

    private fun addBottomDots(currentPage: Int) {
        dots = arrayOfNulls(layouts!!.size)

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

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

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

在A部分,Android Studio给出了以下错误消息:

在TextView类型的可为空的接收器上仅允许安全(?。)或非空声明(!!。)调用。

在B部分:

无法将类型强制转换为“数组”,因为“点”是一个可变属性,可能在此时已更改。

另一种情况:

public class MyViewPagerAdapter extends PagerAdapter {
        private LayoutInflater layoutInflater;

        public MyViewPagerAdapter() {
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(layouts[position], container, false);
            container.addView(view);

            return view;
        }
}

转换成:

inner class MyViewPagerAdapter : PagerAdapter() {
        private var layoutInflater: LayoutInflater? = null

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            layoutInflater = getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE) // part C

            val view = layoutInflater!!.inflate(layouts!![position], container, false)
            container.addView(view)

            return view
        }

在C部分:

类型不匹配。 必填:LayoutInflater? 发现:任何!

如何解决这个问题?

尝试以下

private var dots: ArrayList<TestView> = ArrayList()

private fun addBottomDots(currentPage: Int) {
    val size = layouts?.size ?: 0

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

    dotsLayout?.removeAllViews()

    for (i in 0 until size) {
        val textView = TextView(getContext()) // part A
        textView.text = Html.fromHtml("&#8226;") // part B
        textView.textSize = 35f
        textView.setTextColor(colorsInactive[currentPage])
        dots.add(textView)
        dotsLayout?.addView(dots[i])
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM