kotlin 如何在代码中引用SurfaceView

weylhg0b  于 2023-06-24  发布在  Kotlin
关注(0)|答案(1)|浏览(104)

嗨,我是Kotlinbegginer,我正在尝试创建一个简单的2D图形动画游戏。我正在使用SurfaceView扩展对象来显示我的图像,但是当我尝试引用我的对象时

findViewById<Game>(R.id.gameImageViewwer)

即使游戏对象init节和surfaceCreated方法已经运行,我也会返回null。我的问题是为什么会发生这种情况?我确信我错过了或不理解一些简单的东西
请让我知道,如果我需要分享的代码,我不想超载的问题的一部分
谢谢你的任何帮助
顺便说一句,我试图从onWindowFocusChanged方法访问此对象
布局:

<?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>

活动:

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 -它很长,但我包括init和surfacecreated方法

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())
        }
    }
    }
nlejzf6q

nlejzf6q1#

设法让它工作,我会张贴答案的情况下,有人会有类似的问题:将上部布局更改为:

<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"
            />

另外,在Game类中,不要扩展SurfaceView,而是添加SurfaceView字段,并从xml中创建的字段定义它

相关问题