Android Getpreferences在片段中不工作
像Android Getpreferences在片段中不工作,android,android-studio,android-fragments,kotlin,Android,Android Studio,Android Fragments,Kotlin,像findViewById,getSharedReferences这样的命令在Fragment 我正在使用kotlin,我的代码如下 fun update (v:View){ Val sharedpref = getSharedPreferences("logindata",Context.MODE_PRIVATE)} 日志 您正在调用片段中的上下文对象,片段不是上下文。因此将此行更改为类似以下内容: Val sharedpref = getActivity().getSharedPrefer
findViewById
,getSharedReferences
这样的命令在Fragment
我正在使用kotlin,我的代码如下
fun update (v:View){
Val sharedpref = getSharedPreferences("logindata",Context.MODE_PRIVATE)}
日志
您正在调用
片段
中的上下文
对象,片段不是上下文
。因此将此行更改为类似以下内容:
Val sharedpref = getActivity().getSharedPreferences("logindata",Context.MODE_PRIVATE)}
并使用onCreateView
中的getView
方法来使用findViewById
,例如:
TextView tv = (TextView) getView().findViewById(R.id.mtTextview);
从概念上讲,上述答案()是正确的,但它必须在Kotlin中。像
getActivity()
,getView()
将像属性一样被访问。
而且,它的val
不是val
下面是如何在活动中使用findViewById()
,getSharedReferences()
的简单示例
main活动代码-
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setBaseFragment()
}
private fun setBaseFragment() {
val fragment = MainFragment.newInstance()
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit()
}
}
class MainFragment : Fragment() {
lateinit var show: Button
lateinit var save: Button
lateinit var text: TextView
var prefs: SharedPreferences? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_main, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
show = view?.findViewById(R.id.showButton) as Button
save = view?.findViewById(R.id.saveButton) as Button
text = view?.findViewById(R.id.textResult) as TextView
prefs = activity.getSharedPreferences("FUN",MODE_PRIVATE)
save.setOnClickListener {
val editor = prefs!!.edit()
editor.putString("Saving", "This is saveValueFromSharedPref")
editor.apply()
}
show.setOnClickListener {
text.setText(prefs?.getString("Saving","NotSaved"))
}
}
companion object {
fun newInstance(): MainFragment {
val fragment = MainFragment()
return fragment
}
}
}
这是我的片段类
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setBaseFragment()
}
private fun setBaseFragment() {
val fragment = MainFragment.newInstance()
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit()
}
}
class MainFragment : Fragment() {
lateinit var show: Button
lateinit var save: Button
lateinit var text: TextView
var prefs: SharedPreferences? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_main, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
show = view?.findViewById(R.id.showButton) as Button
save = view?.findViewById(R.id.saveButton) as Button
text = view?.findViewById(R.id.textResult) as TextView
prefs = activity.getSharedPreferences("FUN",MODE_PRIVATE)
save.setOnClickListener {
val editor = prefs!!.edit()
editor.putString("Saving", "This is saveValueFromSharedPref")
editor.apply()
}
show.setOnClickListener {
text.setText(prefs?.getString("Saving","NotSaved"))
}
}
companion object {
fun newInstance(): MainFragment {
val fragment = MainFragment()
return fragment
}
}
}
这是一个带有文本和两个按钮的简单示例
首先,你必须保存并显示
另外,对于应用程序崩溃,您可以检查。尝试使用var而不是val。它是
val
,而不是val
Val
不是类型,只是用于变量(更准确地说:值)声明。Kotlin然后使用类型推断。我照你说的做了,所以studio中不会显示错误,但当我在android手机中运行应用程序时,当我打开碎片时,它会停止工作。在此处添加崩溃日志如何获得崩溃登录安卓studio,工具/android/android设备监视器
从设备选项卡选择您的设备,然后在logcat
部分点击add
按钮,在filter name
中为应用程序设置名称,并按应用程序名称在中添加程序包名称。运行应用程序并查看崩溃报告。如果你正在使用另一个IDE,那么在google中搜索如何在你的IDE中获取应用程序日志