【问题标题】:unresolved reference: activity errors未解决的参考:活动错误
【发布时间】:2019-05-20 20:12:09
【问题描述】:

我正在构建函数以将变量从 editText 字段写入 sharedPreferences。

我尝试了几种写入 sharedPreferences 的可能性,但都没有奏效,因此决定自己编写。因此,我将相关的 TextView 字段设置为 onClick'able,然后运行写入 sharedPreferences 函数。

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) {

    }
    }

我从第 50、54 和 62 行收到“未解决的参考:活动”错误,但不知道如何解决这些错误。

【问题讨论】:

  • 欢迎来到 Stack Overflow!这里没有行号,50、54和62在哪里?你得到了哪些错误?您尝试了哪些“几种可能性”?也请take the tour 和阅读"How do I ask a good question?" 当你有空的时候,它会让我们更容易帮助你。
  • 谢谢。这些行号包含, val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) val sharedPref = activity?.setPreferences(Context.MODE_PRIVATE) ?: return 和 val sharedPref = activity?.setPreferences(Context.MODE_PRIVATE) ?: return 。我从上面发布的 MainActivity.kt 中的这三行中得到了“未解决的参考:活动”错误。我的 git repo 提交说得很好,在我试图实现它的代码中,即我尝试过的那些可能性。

标签: android kotlin sharedpreferences


【解决方案1】:

这段代码有很多地方不太理想,但为了让您开始回答您的直接问题:

Activity 没有您可以引用的 activity 成员(变量或函数)。但是,您尝试引用activity 的点本身就是MainActivity 范围的一部分,并且因为MainActivity 本身就是Activity(通过它从AppCompatActivity 继承),所以您可以简单地将activity. 替换为this.。您可以进一步简化,只需删除这些调用的 this. 前缀即可。

试一试,看看你是否无法从那里弄清楚。如果你不能,我很乐意提供更多帮助!

【讨论】:

  • 使用 'this' 而不是 activity 已经解决了这个错误。然而,我收到以下错误,imagebin.ca/v/4hpY6MAqnwMi,有趣的是不匹配类型错误,现在 chrname 将是一个字符串,但是所有其他的都是整数,因此使用 putInt,但是在变量定义中,我可以使用一些东西喜欢 toInteger() 所以它把它们识别为整数而不是字符串?我还应该指出,我使用了 developer.android.com/training/data-storage/shared-preferences 中的代码来适应我的需要来尝试让它工作。
  • github.com/brobostigon/TW01/blob/master/app/src/main/java/com/… 得到一个工作版本,并将 editText 字段保存到 sharedPreferences。
  • @philiptaylor 对我来说看起来不错!我在那个工作版本中注意到一件事:那些saveXXX(view: View) 函数有很多类似的功能。您可以将该功能重构为一个通用函数。
  • 酷,谢谢,:)。这是个好主意,这就是我编写检索函数的方式,即它在同一个函数中检索它们,
  • 没问题,很高兴能帮上忙!如果您认为我已经帮助您回答了您的问题,您介意接受我的回答,以免其他人认为这是未回答的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 2019-01-24
  • 1970-01-01
  • 2017-08-08
  • 2020-11-24
  • 2021-09-27
  • 1970-01-01
相关资源
最近更新 更多