Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: error: non-static type variable T cannot be referenced from a static context

I have the following class:

@Parcelize
data class Collection<T : Parcelable> constructor(
    var models: List<T>,
    var cursor: String?
) : Parcelable

When I was using Kotlin 1.4.10, the project builds correctly without any errors, then I updated the project to Kotlin 1.4.21, and migrated to use kotlin-parcelize instead of kotlin-android-extensions, so after Kotlin update, when building the project I get the following errors:

> Task :domain:kaptDebugKotlin FAILED
/Library/DevelopmentArea/workspace/baaz_android_new/clean_domain/domain/build/tmp/kapt3/stubs/debug/com/myapp/domain/model/Collection.java:101: error: non-static type variable T cannot be referenced from a static context
        public final com.myapp.domain.model.Collection<T>[] newArray(int size) {
                                                                   ^/Library/DevelopmentArea/workspace/baaz_android_new/clean_domain/domain/build/tmp/kapt3/stubs/debug/com/myapp/domain/model/Collection.java:110: error: non-static type variable T cannot be referenced from a static context
        public final com.myapp.domain.model.Collection<T> createFromParcel(@org.jetbrains.annotations.NotNull()

Note: I'm using Android Studio 4.1.1

like image 409
Mahmoud Jorban Avatar asked Dec 22 '20 10:12

Mahmoud Jorban


People also ask

How do you fix error non static variable this Cannot be referenced from a static context?

Therefore, this issue can be solved by addressing the variables with the object names. In short, we always need to create an object in order to refer to a non-static variable from a static context. Whenever a new instance is created, a new copy of all the non-static variables and methods are created.

Why non static variable Cannot be referenced from a static context?

Contrary to this, non-static variables and methods depend on class instances, as they store and manipulate data specific to individual objects. Therefore, non-static members cannot be accessed from a static context, i.e., there has to be a class instance that references these members.

What does it mean non static method Cannot be referenced from a static context?

A non-static method is dependent on the object. It is recognized by the program once the object is created. But a static method can be called before the object creation. Hence you cannot make the reference.

Can you access non static variable in static context?

Of course, they can but the opposite is not true i.e. you cannot access a non-static member from a static context i.e. static method. The only way to access a non-static variable from a static method is by creating an object of the class the variable belongs.


1 Answers

Currently to fix the errors that I get, and meanwhile, keep using Kotlin 1.4.21, I just removed the @Parcelize annotation from any class with a generic type usage like the one in the question, and just implemented the Parcelable with the old way like the following:

    data class Collection<T> constructor(
    var models: List<T>,
    var cursor: String?
) : Parcelable {

    constructor(parcel: Parcel) : this(
        mutableListOf<T>().also { list: List<T> ->
            parcel.readList(list, Collection<T>::models.javaClass.classLoader)
        },
        parcel.readString())

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeList(models)
        parcel.writeString(cursor)
    }

    override fun describeContents(): Int = 0

    companion object {

        @JvmField
        val CREATOR = object : Parcelable.Creator<Collection<Parcelable>> {
            override fun createFromParcel(source: Parcel): Collection<Parcelable> {
                return Collection(source)
            }

            override fun newArray(size: Int): Array<Collection<Parcelable>?> {
                return arrayOfNulls(size)
            }
        }
    }
}
like image 179
Mahmoud Jorban Avatar answered Sep 19 '22 18:09

Mahmoud Jorban