Hi I am Kotlin begginer and I am trying to create simple game with 2d graphics animation. I am using SurfaceView extended object to show my images however when I try to reference my object with
findViewById<Game>(R.id.gameImageViewwer)
I am getting null back even though Game object init section and surfaceCreated methods have already run. My question is why is this happpening? I am sure I am missing or not understanding something simple
Please let me know if I need to share part of code I dont want to overload question
Thank you for any help
BTW I am trying to access this object from onWindowFocusChanged method
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.imageviewer.Game
        android:id="@+id/gameImageViewwer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity:
package com.example.imageviewer
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import com.example.imageviewer.databinding.ActivityMainBinding
class GameActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var thread: GameThread
    private val mainVM by viewModels<MainViewModelImageViewer>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_game)
    }
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if(hasFocus) {
            findViewById<Game>(R.id.gameImageViewwer).startGame()
            findViewById<Game>(R.id.gameImageViewwer).resumeGame()
        }
        else {
            mainVM.save(findViewById<Game>(R.id.gameImageViewwer).save3iClickCount(),findViewById<Game>(R.id.gameImageViewwer).save4iNumberOfRemoved())
            findViewById<Game>(R.id.gameImageViewwer).pauseGame()
        }
    }
   }
SurfaceView - it is quite long but I am including init and surfacecreated method
class Game(context: Context, attributes: AttributeSet) : SurfaceView(context), SurfaceHolder.Callback {
init {
        holder.addCallback(this)
        thread = GameThread(holder,this)
    }
    override fun surfaceCreated(holder: SurfaceHolder) {
        Log.d("DEBBIE","SurfCreated")
        try {
            //thread.setRunning
        }
        catch (e: IllegalThreadStateException) {
            Log.d("EXCEPTION","surfaceCreated: " + e.toString())
        }
    }
    }
