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

时间:2018-06-28 19:18:02

标签: java android 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的可为null的接收器?

在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?找到:任何!

该如何解决?

1 个答案:

答案 0 :(得分:2)

尝试以下

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