Inaccessible methods from application class (kotlin)

0

As it turns out, from the class that extends from Application () I access the other classes of the project but I do not see their methods (unresolved reference).

This is the application class:

package com.fernando.lanatelar
import android.app.Application

class LanaTelarApp : Application() {

    companion object Constants {
        const val TAG = "ObjectBoxExample"
    }

    override fun onCreate() {
        super.onCreate()
        ObjectBox.construye(this)
    }
}

And this is the ObjectBox class:

package com.fernando.lanatelar

import android.content.Context
import com.fernando.lanatelar.modelo.MyObjectBox
import io.objectbox.BoxStore

class ObjectBox {
    lateinit var boxStore: BoxStore
        private set

    fun construye(context: Context) {
        boxStore = MyObjectBox.builder().androidContext(context.applicationContext).build()
    }
}

From LanaTelarApp I access the ObjectBox without problems, but its methods do not appear and when I put ObjectBox.build () it gives me an error of: Unresolved reference.

The manifest is like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fernando.lanatelar">
    <application
        android:name=".LanaTelarApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
    
asked by Fer Nando 02.07.2018 в 09:21
source

2 answers

0

As you have indicated, if you do not instantiate the class, the only way is to access a static method, which must be within the companion object. If you do not want to create a static method, you need to instantiate the class:

val objectBox = ObjectBox()

and now you can call the method:

 objectBox.construye()
    
answered by 17.09.2018 в 16:52
-1

Do you mean static methods? This is declared by declaring the method within a block companion object . This is equivalent to static in Java:

class ObjectBox {
    lateinit var boxStore: BoxStore
    private set

    companion object  {

        fun construye(context: Context) {
            boxStore = MyObjectBox.builder().androidContext(context.applicationContext).build()
        }
    } 
}

Then you can access the method build using the class as an identifier:

  // ...
    ObjectBox.construye(this);
  //...
    
answered by 02.07.2018 в 15:17