简体   繁体   中英

Update textView text from class companion object

I'm trying to update a textView from a class's companion object and the application crashes. What am I doing wrong?

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.test.testapp.classes.ExampleClass
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        testMessage.text = "1"
        ExampleClass.writeText("2")
    }
}

ExampleClass.kt

import com.test.testapp.MainActivity
import kotlinx.android.synthetic.main.activity_main.*

class ExampleClass {
    companion object{
        fun writeText(textValue:String) {
            MainActivity().testMessage.text = textValue
        }
    }
}

Android studio error message:

FATAL EXCEPTION: main Process: com.test.testapp, PID: 15819
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testapp/com.test.testapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

Because with the code MainActivity()... you aren't getting the Activity that has been loaded, but you are constructing a new Activity that hasn't been shown yet so the view doesn't exist.

There are various ways to achieve what you want, even if the flow is wrong

example

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        testMessage.text = "1"
        ExampleClass.writeText("2",this)
    }
}

class ExampleClass {
    companion object{
        fun writeText(textValue:String,mainActivity:MainActivity) {
            mainActivity.testMessage.text = textValue
        }
    }
}

I don't know exactly why do you want to do that but if you want to pass data between activities or fragments or services check that https://developer.android.com/guide/components/intents-filters

inside the writeText (textValue: String) method, you create a new instance of MainActivity (MainActivity()) in which textView is null , and not get the existing one

you should not use companion object for this. Сould explain the situation, why do you need it

if it necessary , you can do so:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.test.testapp.classes.ExampleClass
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        testMessage.text = "1"
        ExampleClass.writeText("2", testMessage)
    }
}

ExampleClass.kt

import com.test.testapp.MainActivity
import kotlinx.android.synthetic.main.activity_main.*

class ExampleClass {
    companion object{
        fun writeText(textValue:String, textView: TextView) {
            textView.text = textValue
        }
    }
}

Try like this

Pass MainActivity Object to ExampleClass

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        testMessage.text = "1"
        ExampleClass.writeText("2",this)
    }
}

And use MainActivity object to access its properties.

class ExampleClass {
    companion object{
        fun writeText(textValue:String,mainActivity: MainActivity) {
            mainActivity.testMessage.text = textValue
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM