class ProfileFragment : Fragment() {
private lateinit var tvhelpcenter: TextView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_profile, container, false)
/* val view = inflater?.inflate(
R.layout.fragment_home,
container, false
)*/
/*val tv_help_center: TextView? = view?.findViewById(R.id.tv_help_center)
tv_help_center!!.setOnClickListener {
requireActivity().run {
startActivity(Intent(this, HelpCenterActivity::class.java))
finish()
}
}*/
}
}
答案 0 :(得分:2)
您应该使用 activity!!
代替此处的 this 。
对于片段使用-> 活动!
activity!!.startActivity(Intent(activity!!, HelpCenterActivity::class.java))
finish()
第二种方法
(activity as MainActivityName)?.let{
val intent = Intent (it, HelpCenterActivity::class.java)
it.startActivity(intent)
finish()
}
let
->将调用它的对象作为参数并返回 lambda表达式的结果。
it
->关键字包含let
中属性的副本。
答案 1 :(得分:0)
致电父活动:
val intent = Intent(getActivity(),Main :: class.java) getActivity()。startActivity(intent)
活动?.let { val intent =目的(it,Main :: class.java) it.startActivity(intent) }