Kotlin 安全调用 (?.) 实现
Kotlin safe call (?.) implementation
我可以用繁琐的方法解决它,所以如果您不post解决方法而是记录在案的解决方案,我将不胜感激。
我正在尝试将以下代码片段添加到片段的 onCreateView()
中,但是 "thanks" 安全调用(或我的代码设计)事情变得复杂了。 TabLayoutMediator(activity
部分 Kotlin 显示在 activity
之后添加安全调用 (?.
) 的警告(相当于 Java 中的 getActivity
)可能是 null
.
Original code snippet before trying any solution
TabLayoutMediator(activity.findViewById(R.id.tabs), view.findViewById(R.id.view_pager),
TabLayoutMediator.OnConfigureTabCallback { tab, position ->
// Styling each tab here
tab.text = mValueHeadlineCategoryList[position]
}).attach()
1) Trying to solve by adding safe call
(failed)
现在添加安全调用违反了 TabLayoutMediator
输入参数,我在 TabLayoutMediator
上收到 type mismatch Required:TabLayout Found:TabLayout?
警告。
TabLayoutMediator(activity?.findViewById(R.id.tabs), view.findViewById(R.id.view_pager),
TabLayoutMediator.OnConfigureTabCallback { tab, position ->
tab.text = mValueHeadlineCategoryList[position]
}).attach()
2) Trying to solve by null check before safe call
(failed)
添加空值检查,但现在 Kotlin 在 TabLayoutMediator(activity
部分发出警告 Smart cast to 'FragmentActivity' is impossible, because 'activity' is a property that has open or custom gette
if (activity != null) {
TabLayoutMediator(activity.findViewById(R.id.tabs), view.findViewById(R.id.view_pager),
TabLayoutMediator.OnConfigureTabCallback { tab, position ->
tab.text = mValueHeadlineCategoryList[position]
}).attach()
}
替换activity
with requireActivity()
.
简单修复:您可以这样做
activity?.let { instance ->
TabLayoutMediator(instance.findViewById(R.id.tabs), view.findViewById(R.id.view_pager),
TabLayoutMediator.OnConfigureTabCallback { tab, position ->
tab.text = mValueHeadlineCategoryList[position]
}).attach()
}
我可以用繁琐的方法解决它,所以如果您不post解决方法而是记录在案的解决方案,我将不胜感激。
我正在尝试将以下代码片段添加到片段的 onCreateView()
中,但是 "thanks" 安全调用(或我的代码设计)事情变得复杂了。 TabLayoutMediator(activity
部分 Kotlin 显示在 activity
之后添加安全调用 (?.
) 的警告(相当于 Java 中的 getActivity
)可能是 null
.
Original code snippet before trying any solution
TabLayoutMediator(activity.findViewById(R.id.tabs), view.findViewById(R.id.view_pager),
TabLayoutMediator.OnConfigureTabCallback { tab, position ->
// Styling each tab here
tab.text = mValueHeadlineCategoryList[position]
}).attach()
1) Trying to solve by adding
safe call
(failed)
现在添加安全调用违反了 TabLayoutMediator
输入参数,我在 TabLayoutMediator
上收到 type mismatch Required:TabLayout Found:TabLayout?
警告。
TabLayoutMediator(activity?.findViewById(R.id.tabs), view.findViewById(R.id.view_pager),
TabLayoutMediator.OnConfigureTabCallback { tab, position ->
tab.text = mValueHeadlineCategoryList[position]
}).attach()
2) Trying to solve by null check before
safe call
(failed)
添加空值检查,但现在 Kotlin 在 TabLayoutMediator(activity
部分发出警告 Smart cast to 'FragmentActivity' is impossible, because 'activity' is a property that has open or custom gette
if (activity != null) {
TabLayoutMediator(activity.findViewById(R.id.tabs), view.findViewById(R.id.view_pager),
TabLayoutMediator.OnConfigureTabCallback { tab, position ->
tab.text = mValueHeadlineCategoryList[position]
}).attach()
}
替换activity
with requireActivity()
.
简单修复:您可以这样做
activity?.let { instance ->
TabLayoutMediator(instance.findViewById(R.id.tabs), view.findViewById(R.id.view_pager),
TabLayoutMediator.OnConfigureTabCallback { tab, position ->
tab.text = mValueHeadlineCategoryList[position]
}).attach()
}