Why can't I access a component from my xml file by declaring an object globally?

Pushkal Mondal

So here is the code in my MainActivity.kt file

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast

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

    var tvInput: TextView = findViewById(R.id.tvInput)

    fun onDigit(view: View){
        tvInput.append((view as Button).text)
    }

    fun onClear(view: View){
        tvInput.text = ""
    }
}

When I declare the object "tvInput" globally like I have done above, it keeps crashing the app, however when I declare it separately for each function it runs smoothly, How can I fix this?

Tenfour04

findViewById() searches the current layout for the view and throws an exception if it isn't found. But there is no current layout yet at the time the Activity class is instantiated. There is only a layout after you have called setContentView() in the onCreate() function. Your tvInput property in your example is calling findViewById() at the time the class is instantiated.

If you want to delay when it first calls findViewById(), you can use a Lazy delegate like this, and it will resolve the issue:

val tvInput: TextView by lazy { findViewById(R.id.tvInput) }

Now it won't call findViewById() until the first time you actually use the property, which will happen after you call setContentView().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't I access "fullName" from my object

Can't access my store object from my component

Why can't I access props within my component?

Why can't I access the xhr object from inside my onreadystatechange handler?

Why can't I access variable in my dictionary of JSON object?

Why can't i import my component from the same folder?

Why can't I access local class outside declaring method?

Why can't I import a React component from a file?

why can't I access my postgres from remote?

Why I can't access my script variable from class?

Why can't I access my data from Firestore?

Why can't I access a constant from an object?

Why i can`t change my object with .map from mongo?

Why can't I access javascript function from HTML file?

Why can't I retrieve data from my Json file?

Why can't I delete this file from my Xcode project?

Why can't I access my dynamically inserted attribute in my Object via Vue.js?

Why can I access a component variable only from outside the component?

Why can't I import my component into my app?

Can't access component method inside my blade file

Why can't i access my public IP-Adress from outside my LAN?

Why can't I access to my data function from my computed property?

Why can't I update the state of my component from within a useEffect callback?

Can't access global SASS variables from my component

Can't resolve component vue file from my router file

How can i access my WebView from another component?

I can't make my app use the navigationOptions from the component file instead of the routes

Why I can't access template DOM element in the constructor of the component

Why can't I access the props directly in Child Component?