0

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())
        }
    }
    }
  • Please share more code, maybe also the layout file – Remc4 Jun 05 '23 at 10:55
  • You have `ActivityMainBinding`, but you don't use it. If you're using DataBinding, call `DataBindingUtil.setContentView()` in `onCreate()` and then use `binding.gameImageViewwer` everywhere instead of `findViewById()`. – Remc4 Jun 05 '23 at 11:46
  • When I try to inflate with binding my application stops (I assumed it was because surfaceView is created while window displaying it is visible) -> when calling this line binding = ActivityGameBinding.inflate(layoutInflater) I get a call from init from Game object and then it stops – duperele 11 Jun 05 '23 at 11:56
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imageviewer/com.example.imageviewer.GameActivity}: java.lang.NullPointerException: Missing required view with ID: com.example.imageviewer:id/gameImageViewwerCaused by: java.lang.NullPointerException: Missing required view with ID: com.example.imageviewer:id/gameImageViewwer – duperele 11 Jun 05 '23 at 12:24
  • This seems to be the root cause of your error. Try going through the answers posted here: https://stackoverflow.com/q/58554751/971972 – Remc4 Jun 05 '23 at 15:41

1 Answers1

0

Managed to get it working I will post answer in case someone will have similar issue: Change upper layout to :

        <SurfaceView
            android:id="@+id/ububiiuni"
            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"
            />

Also in Game class dont extend SurfaceView, add SurfaceView field instead and define it from the one created in xml