Reputation: 27
I am building functions to write variables from editText fields to sharedPreferences.
I have tried several possibilities for writing to sharedPreferences, and none have worked so decided to write my own. So i have made the related TextView fields onClick'able, which then runs the write to sharedPreferences functions.
package com.taylorworld.tw01
import android.content.Context
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu
import android.view.MenuItem
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
class MainActivity() : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//setSupportActionBar(findViewById(R.id.my_toolbar))
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
var strength = editText2.text.toString()
var dexterity = editText.text.toString()
var intelligance = editText4.text.toString()
var wisdom = editText3.text.toString()
var constitution = editText6.text.toString()
var charisma = editText5.text.toString()
var chrname = editText7.text.toString()
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
public fun saveStr(view: View) {
val sharedPref = activity?.setPreferences(Context.MODE_PRIVATE) ?: return with(sharedPref.edit()) {
putInt(getString(STR), strength)
commit()
}
}
public fun saveDex(view: View) {
val sharedPref = activity?.setPreferences(Context.MODE_PRIVATE) ?: return with(sharedPref.edit()) {
putInt(getString(DEX), dexterity)
commit()
}
}
public fun readStr(view: View) {
}
}
I get 'unresolved reference: activity' errors from line 50, 54 and 62, and cant work out how to resolve the errors.
Upvotes: 1
Views: 8947
Reputation: 771
There are a number of things that aren't quite ideal about this code, but to get you started with an answer to your immediate question:
Activities don't have an activity
member (variable or function) that you can reference. However, the points at which you're trying to reference activity
are themselves part of MainActivity
's scope and because MainActivity
is itself an Activity
(via its inheritance from AppCompatActivity
), so you can simply replace activity.
with this.
. You can simplify even further and just delete the this.
prefix for those calls, as well.
Give that a shot and see if you can't figure it out from there. If you can't, I'll be glad to help some more!
Upvotes: 3