gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:04:00 28 4
gpt4 key购买 nike

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 给出了以下错误消息:

Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type TextView?

B 部分:

Smart cast to 'Array' is impossible, because 'dots' is a mutable property that could have been changed by this time.

另一个案例:

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 部分:

Type mismatch. Required: LayoutInflater? Found: Any!

如何解决这个问题?

最佳答案

尝试以下操作

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

关于java - 需要从 Java 到 Kotlin 的正确转换的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51089926/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com