我已经在ViewPager中创建了一个Fragment。片段加载正常。 我正在使用一个按钮在viewpager中滑动片段,而在滑动之前,我想检查验证。所以我在Activity中做到了:
val pos: Int = viewPager.currentItem
val activeFragment: Fragment? = mAdapter.getItem(pos)
if (viewPager.currentItem == 0) {
if (pos == 0) (activeFragment as FragmentOne).checkValidation()
下面是我的片段代码。
class FragmentOne: BaseFragment() {
fun newInstance(): FragmentOne{
val args = Bundle()
val fragment = FragmentOne()
fragment.setArguments(args)
return fragment
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view : View = inflater.inflate(R.layout.fragment_one, container, false);
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
initiateUI()
}
fun checkValidation(): Boolean {
if (TextUtils.isEmpty(etDateOfBirth.text.toString())) {
showMessage(etDateOfBirth, getString(R.string.str_empty_dob))
return false
}
return true
}
}
当我按下一步时,我得到错误: java.lang.IllegalStateException:etDateOfBirth不能为空 有人可以帮忙吗?
答案 0 :(得分:2)
您不是定义etDateOfBirth吗?在onViewCreated()上定义编辑文本
EditText etDateOfBirth = view.findViewById(R.id.sample);