Home > Net >  I want to make this boolean value true when I click this button. And pass it to a function to displa
I want to make this boolean value true when I click this button. And pass it to a function to displa

Time:03-19

This is the code for the button

class NewGame : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_new_game)
    val results = findViewById<TextView>(R.id.textView5)
    val results2 = findViewById<TextView>(R.id.textView6)
    val submit = findViewById<Button>(R.id.button7)
    var hasclicked = false



    submit.setOnClickListener {
        hasclicked = true

    }
    game (result, hasclicked)

This is the code for the function

fun game(
     result: TextView, hasclicked : Boolean ) {
  



    if (hasclicked == true){
        result.setText("correct")
    }

    

But when I run this and click on the button nothing appears on screen. Can someone help me with this.

CodePudding user response:

When clicking, you are only assigning true to boolean but not calling the game function:

It should be like this:

submit.setOnClickListener {
    hasclicked = true
    game(result, hasclicked)
}

CodePudding user response:

We get this error when we just set a value but do not try using it.

Here if you see your code, you are setting the value. But, not getting it.

By accessed it means that you are not getting it. Only setting it.

You can just choose to ignore it in some cases where it actually make sense,

enter image description here

What you can do is hover the affected area and press Alt Enter in "Windows" or alt/option/⌥ Enter in "Mac", and chose to suppress it.

You'll see,

enter image description here

  • Related