【发布时间】:2022-01-02 10:56:08
【问题描述】:
我正在通过共享首选项从活动中获取片段中的数据,但我无法执行该任务。
class ProfileFragment : Fragment() {
lateinit var sharedPreference: SharedPreferences
lateinit var txtName:TextView
lateinit var txtEmail:TextView
lateinit var txtMobileNo:TextView
lateinit var txtAddress:TextView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_profile, container, false)
sharedPreference = this.requireActivity().getSharedPreferences(getString(R.string.preference_file_name), Context .MODE_PRIVATE)
txtName = view.findViewById(R.id.title)
txtEmail = view.findViewById(R.id.Semail)
txtMobileNo = view.findViewById(R.id.Smobile)
txtAddress = view.findViewById(R.id.Sadress)
txtName.setText(sharedPreference.getString("title", "default")).toString()
txtName.setText(sharedPreference.getString("Email", "default")).toString()
txtName.setText(sharedPreference.getString("Mobile", "default")).toString()
txtName.setText(sharedPreference.getString("delivery", "default")).toString()
return view
}
}
【问题讨论】: