【问题标题】:False unresolved reference in Android StudioAndroid Studio 中的错误未解析引用
【发布时间】:2020-04-10 17:37:55
【问题描述】:

我刚刚在 Android Studio 上构建了我的第一个应用程序。一切都很好,直到几天后进行了一些更改,我收到了更新通知并继续进行操作,结果我开始收到 unresolved reference 错误。

即使应用程序可以构建和运行良好,但问题是我创建的所有其他项目都遵循同样的错误。

我已经尝试了几个建议,但无济于事。 我删除了idea,从另一个导入了项目,使缓存失效并重新启动,与系统同步并与gradle同步。

我目前正在尝试在一个新项目中使用 Room,当我尝试为我的数据库创建单例时,synchronized 被标记为 unresolved reference

我还注意到我的文件总是损坏,我必须返回 Git-hub 重新复制这些文件,即使它们在编译时没有出错

总的来说,我认为这些是当前项目和以前项目都面临的问题 1.unresolved reference

以前的项目

        val sharedPref = activity!!.getPreferences(Context.MODE_PRIVATE)

        val none = resources.getString(R.string.none)

        val countries = arrayListOf<String>("Nigeria", "Zambia", "Ethiopia", "Ghana", none)
        val greenWallCountries = arrayListOf<String>("Great green")

        Collections.sort(countries, String.CASE_INSENSITIVE_ORDER)

        val adapter = ArrayAdapter(context!!, R.layout.spinner_item, countries)
        locationSpinner.adapter = adapter



        locationSpinner.isEnabled = false
        var chosen: String? = null
        greenwallVG.setOnClickListener {

            chosen = "ggw"
            greenwallVG.setBackgroundColor(R.color.primaryTransparent)
            countriesVG.setBackgroundColor(R.color.colorNeutral)

            locationSpinner.isEnabled = false

            Toast.makeText(context!!, "$chosen", Toast.LENGTH_LONG).show()
        }

当前项目

**synchronized 在这里标记为 unresolve reference

package com.example.contactmvvm

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import kotlinx.coroutines.internal.synchronized

@Database(entities = [ContactEntity::class], version = 1)
abstract class ContactDatabase : RoomDatabase() {

    abstract fun getContactDao():ContactDao

    companion object{
        private var instance:ContactDatabase?=null

        fun getInstance(context: Context) =
            instance?: synchronized(this){
                instance?: Room.databaseBuilder(context.applicationContext,
                ContactDatabase::class.java, "contact_database")
                    .fallbackToDestructiveMigration()
                    .build()


            }

    }

}
  1. 错误:找不到字段的吸气剂。 私人 int id;
    package com.example.contactmvvm

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "contacts_table")
class ContactEntity(
    val firstName:String,
    val lastName:String,
    val sex: String,
    val address:String
    ) {
    @PrimaryKey(autoGenerate = true)
    private var id:Int = 0


}

是否还有其他措施可以消除此错误。

【问题讨论】:

标签: android android-studio kotlin


【解决方案1】:

synchronized 关键字在 Kotlin 中已被弃用。

虽然写得很清楚:

UnsupportedOperationException - 总是

所以,就算知道了,也没什么用。

kotlinx.coroutines.internal.synchronized 读作“内部”,显然是别的东西。最好的选择可能是Coroutines.Sync.Mutex,一旦synchronized 将不再存在。

【讨论】:

  • 这是否意味着我必须将协程添加到我的依赖项中?
  • @Darotudeen 你显然已经添加了它们......虽然我宁愿认为它应该在没有同步的情况下工作,因为在 Java 中它也可以在没有同步的情况下工作。
  • 这是一个有效的example(它使用了已弃用的synchronized 关键字)......但它可能会抛出一个UnsupportedOperationException。该问题没有说明您正在使用哪些 Kotlin 版本 - 较新的版本可能会抱怨 - 甚至不再知道它。降级到一些过时的 Kotlin 版本应该会让人知道,但这仍然是一种次优方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多