1

I just built my first App on Android studio. Everything was fine until few days while making some changes I got a notification for update and went on with it only for me to start getting unresolved reference error.

Even with that the application would build and run well but the problem is every other project I created after that follow suit with the same error.

I have followed tried several advice but to no avail. I have deleted idea, imported the project from another one, done invalidate cache and restart, sync with system and sync with gradle.

I am currently trying to use Room on a new project and synchronized is marked unresolved reference as I tried to create singleton for my database.

I also noticed that my files are always corrupted, I have to go back to Git-hub to recopy those files even though they don't give error on compilation

In all I think these are the issues am facing both on current project and previous project 1. the unresolved reference

Previous Project

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

Current Project

**synchronized is marked unresolve reference here

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. error: Cannot find getter for field. private 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


}

Is there something else to be done to annul this error.

Henry
  • 42,982
  • 7
  • 68
  • 84
Darotudeen
  • 1,914
  • 4
  • 21
  • 36
  • Maybe related to this error in Android Studio 4 Canary 6 - https://stackoverflow.com/questions/57707769/unresolved-reference-in-wrong-test-scope-android-studio-3-5/59311273#59311273 – Bohsen Dec 18 '19 at 07:45

1 Answers1

0

The synchronized keyword had been deprecated in Kotlin.

While it clearly reads:

UnsupportedOperationException - always

Therefore, even if it would be known, it would still be quite useless.

kotlinx.coroutines.internal.synchronized reads "internal" and obviously is something else. The best alternative might be Coroutines.Sync.Mutex, once synchronized won't exist anymore.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Does this mean I have to add coroutines to my dependencies? – Darotudeen Dec 18 '19 at 07:57
  • @Darotudeen you've obviously already added them... while I rather think it should work without synchronization, because in Java it also works without doing so. – Martin Zeitler Dec 18 '19 at 08:05
  • Here's a working [example](https://gist.github.com/TannerGabriel/f5594ad3b2c9f2586f2275be5103b29e#file-appdatabase-kt) (which uses the deprecated `synchronized` keyword)... but it might throw a `UnsupportedOperationException`. The question does not state which Kotlin versions you are using - and newer versions might complain - or not even know it anymore. Downgrading to some outdated Kotlin version should make it known, but is still a sub-optimal approach. – Martin Zeitler Dec 22 '19 at 19:36