Paramjeet Singh
Paramjeet Singh

Reputation: 3090

Getpreferences not working in fragment

Commands like findViewById , getSharedPreferences are not working inside Fragment I am using kotlin and my code is as follow

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)

Upvotes: 3

Views: 1690

Answers (2)

Arpit
Arpit

Reputation: 1289

Thou concept wise the above answer (https://stackoverflow.com/a/44469679/3845798) is correct, but there it needs to be in Kotlin. Like getActivity(), getView() will be access like a property. Also, its val not Val.

Here is simple example of how to use findViewById(), getSharedPreferences() inside the activity.

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

And this is my fragment Class

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

It's a simple example with a text and two button.

First you have to save and show.

Also, for your app crash you can check this solution.

Upvotes: 0

S.R
S.R

Reputation: 2829

You are calling a Context object in Fragment, Fragment is not a Context.so change this line to something like this:

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

And use getView method in onCreateView for using findViewById, for example:

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

Upvotes: 2

Related Questions