Getpreferences无法在片段中工作

时间:2017-06-10 02:43:12

标签: android android-studio android-fragments kotlin

findViewByIdgetSharedPreferences等命令在Fragment内无效 我正在使用kotlin,我的代码如下

fun update (v:View){
Val sharedpref = getSharedPreferences("logindata",Context.MODE_PRIVATE)}

LOG

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.techno.app, PID: 25691
java.lang.IllegalStateException: Could not find method update(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5721)
at android.widget.TextView.performClick(TextView.java:10936)
at android.view.View$PerformClick.run(View.java:22620)
at android.os.Handler.handleCallback(Handler.java:739)

2 个答案:

答案 0 :(得分:1)

您正在Context中调用Fragment对象,片段不是Context。因此请将此行更改为以下内容:

Val sharedpref = getActivity().getSharedPreferences("logindata",Context.MODE_PRIVATE)}

并使用getView中的onCreateView方法来使用findViewById,例如:

TextView tv = (TextView) getView().findViewById(R.id.mtTextview);

答案 1 :(得分:0)

你明智的概念上面的答案(https://stackoverflow.com/a/44469679/3845798)是正确的,但它需要在Kotlin中。与getActivity()一样,getView()将像财产一样进行访问。 此外,其val不是Val

以下是如何在活动中使用findViewById()getSharedPreferences()的简单示例。

MainActivity Code -

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
        }
    }
}

这是一个带文字和两个按钮的简单例子。

首先你必须保存并显示。

此外,对于您的应用崩溃,您可以查看this solution