如何在代码中引用我的 SurfaceView

huangapple go评论66阅读模式
英文:

How do I reference my SurfaceView in code

问题

你好,你提供的内容中有一些代码部分,我将只翻译文本部分,不包括代码。以下是翻译后的文本:

Hi,我是 Kotlin 初学者,正在尝试创建一个包含 2D 图形动画的简单游戏。
我正在使用扩展了 SurfaceView 的对象来显示我的图像,但是当我尝试用以下方式引用我的对象时:

findViewById<Game>(R.id.gameImageViewwer)

即使 Game 对象的初始化部分和 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 - 这部分代码很长,但我包括了初始化和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())
        }
    }
}

希望这对你有帮助。如有其他问题,请随时提出。

英文:

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&lt;Game&gt;(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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;com.example.imageviewer.Game
        android:id=&quot;@+id/gameImageViewwer&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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&lt;MainViewModelImageViewer&gt;()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_game)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if(hasFocus) {
            findViewById&lt;Game&gt;(R.id.gameImageViewwer).startGame()
            findViewById&lt;Game&gt;(R.id.gameImageViewwer).resumeGame()
        }
        else {
            mainVM.save(findViewById&lt;Game&gt;(R.id.gameImageViewwer).save3iClickCount(),findViewById&lt;Game&gt;(R.id.gameImageViewwer).save4iNumberOfRemoved())
            findViewById&lt;Game&gt;(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(&quot;DEBBIE&quot;,&quot;SurfCreated&quot;)

        try {
            //thread.setRunning

        }
        catch (e: IllegalThreadStateException) {
            Log.d(&quot;EXCEPTION&quot;,&quot;surfaceCreated: &quot; + e.toString())
        }
    }
    }

答案1

得分: 0

成功使其工作,我将在此发布答案以供有类似问题的人参考:
将上层布局更改为:

<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中创建的字段中定义它。

英文:

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

        &lt;SurfaceView
            android:id=&quot;@+id/ububiiuni&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
            app:layout_constraintEnd_toEndOf=&quot;parent&quot;
            app:layout_constraintStart_toStartOf=&quot;parent&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;
            /&gt;

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

huangapple
  • 本文由 发表于 2023年6月5日 18:51:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定