英文:
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<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())
}
}
}
答案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 :
<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论