如何使用 Kotlin 在 android 迷宫游戏中修复 Null 不能是非空类型的值
How to fix Null can not be a value of a non-null type in android maze game with Kotlin
我按照本教程在 android 中创建了一个迷宫,但是当我在 getNeighbour 方法 return 为 null 时出现错误
错误:Null 不能是非空类型 Cell 的值
感谢任何对我做错的帮助。
我用 Cell 试过了?但无法让它工作,它给了我更多的错误
这是我的代码:
class GameView: View{
//revisar por que hace cosas raras con los limites de cols y rows
private val COLS:Int = 7
private val ROWS: Int = 12
private var cellSize: Float = 0F
private var hMargin: Float = 0F
private var vMargin: Float = 0F
private val wallPaint = Paint()
private val random:Random = Random()
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs){
wallPaint.color = Color.WHITE
wallPaint.strokeWidth = 1F
createMaze()
}
fun getNeighbour(cell:Cell): Cell {
var vecinos: ArrayList<Cell> = ArrayList()
//vecino izquierda
if(cell.col > 0) {
if (!cells[cell.col - 1][cell.row].visited) {
vecinos.add(cells[cell.col - 1][cell.row])
}
}
//vecino derecha
if(cell.col < COLS - 1) {
if (!cells[cell.col + 1][cell.row].visited) {
vecinos.add(cells[cell.col + 1][cell.row])
}
}
//vecino arriba
if(cell.row > 0) {
if (!cells[cell.col][cell.row - 1].visited) {
vecinos.add(cells[cell.col ][cell.row - 1])
}
}
//vecino abajo
if(cell.row < ROWS - 1) {
if (!cells[cell.col][cell.row + 1].visited) {
vecinos.add(cells[cell.col][cell.row + 1])
}
}
if (vecinos.size > 0) {
var index = random.nextInt(vecinos.size)
return vecinos[index]
}else {
// i get this error: Null can not be a value of a non-null type
// Cell
return null
}
}
fun removeWall(current:Cell,next:Cell){
if (current.col == next.col && current.row == next.row +1){
current.topWall = false
next.bottomWall = false
}
if (current.col == next.col && current.row == next.row -1){
current.bottomWall = false
next.topWall = false
}
if (current.col == next.col + 1 && current.row == next.row){
current.leftWall = false
next.rightWall = false
}
if (current.col == next.col - 1 && current.row == next.row +1){
current.rightWall = false
next.leftWall = false
}
}
var cells: Array<Array<Cell>> = Array(COLS, {Array(ROWS, {Cell()})})
fun createMaze(){
var stack = Stack<Cell>()
var current:Cell
var next:Cell
for(x in 0 until COLS){
for(y in 0 until ROWS){
cells[x][y] = Cell(x,y)
}
}
current = cells[0][0]
current.visited = true
do{
next = getNeighbour(current)
removeWall(current,next)
stack.push(current)
current = next
current.visited = true
}while (!stack.empty())
}
override fun onDraw(canvas: Canvas) {
canvas.drawColor(Color.DKGRAY)
val ancho = width
val alto = height
println(ancho)
println(alto)
if(ancho/alto < COLS/ROWS){
cellSize = ancho/(COLS+1).toFloat()
}else{
cellSize = alto/(ROWS+1).toFloat()
}
hMargin = (ancho - COLS*cellSize)/2
vMargin = ((alto - ROWS*cellSize)/2)
canvas.translate(hMargin,vMargin)
for(x in 0 until COLS){
for(y in 0 until ROWS){
if (cells[x][y].topWall){
canvas.drawLine(
x*cellSize,
y*cellSize,
(x+1)*cellSize,
y*cellSize,
wallPaint)
}
if (cells[x][y].leftWall){
canvas.drawLine(
x*cellSize,
y*cellSize,
x*cellSize,
(y+1)*cellSize,
wallPaint)
}
if (cells[x][y].bottomWall){
canvas.drawLine(
x*cellSize,
(y+1)*cellSize,
(x+1)*cellSize,
(y+1)*cellSize,
wallPaint)
}
if (cells[x][y].rightWall){
canvas.drawLine(
(x+1)*cellSize,
y*cellSize,
(x+1)*cellSize,
(y+1)*cellSize,
wallPaint)
}
}
}
}
}
class Cell(var col:Int = 0, var row: Int = 0 ){
var topWall = true
var leftWall = true
var bottomWall = true
var rightWall = true
var visited = false
}
您方法的 return 类型不允许 null
被 returned。您需要将其更改为可空 Cell?
(可空性由问号表示)。
fun getNeighbour(cell:Cell): Cell? {
var vecinos: ArrayList<Cell> = ArrayList()
.....
.....
从语法上看
// first change
fun getNeighbour(cell:Cell): Cell -> fun getNeighbour(cell:Cell): Cell?
// second change
var next:Cell -> var next:Cell? = null
// third change
next = getNeighbour(current)
if(next != null) {
// you process code goes here
}
但是从设计的角度来说,fun getNeighbour(cell:Cell): Cell
意味着总能找到邻居。
如果不是这种情况,您应该将其更改为 Cell?
并且以下代码应处理 null 情况。
我按照本教程在 android 中创建了一个迷宫,但是当我在 getNeighbour 方法 return 为 null 时出现错误
错误:Null 不能是非空类型 Cell 的值
感谢任何对我做错的帮助。
我用 Cell 试过了?但无法让它工作,它给了我更多的错误
这是我的代码:
class GameView: View{
//revisar por que hace cosas raras con los limites de cols y rows
private val COLS:Int = 7
private val ROWS: Int = 12
private var cellSize: Float = 0F
private var hMargin: Float = 0F
private var vMargin: Float = 0F
private val wallPaint = Paint()
private val random:Random = Random()
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs){
wallPaint.color = Color.WHITE
wallPaint.strokeWidth = 1F
createMaze()
}
fun getNeighbour(cell:Cell): Cell {
var vecinos: ArrayList<Cell> = ArrayList()
//vecino izquierda
if(cell.col > 0) {
if (!cells[cell.col - 1][cell.row].visited) {
vecinos.add(cells[cell.col - 1][cell.row])
}
}
//vecino derecha
if(cell.col < COLS - 1) {
if (!cells[cell.col + 1][cell.row].visited) {
vecinos.add(cells[cell.col + 1][cell.row])
}
}
//vecino arriba
if(cell.row > 0) {
if (!cells[cell.col][cell.row - 1].visited) {
vecinos.add(cells[cell.col ][cell.row - 1])
}
}
//vecino abajo
if(cell.row < ROWS - 1) {
if (!cells[cell.col][cell.row + 1].visited) {
vecinos.add(cells[cell.col][cell.row + 1])
}
}
if (vecinos.size > 0) {
var index = random.nextInt(vecinos.size)
return vecinos[index]
}else {
// i get this error: Null can not be a value of a non-null type
// Cell
return null
}
}
fun removeWall(current:Cell,next:Cell){
if (current.col == next.col && current.row == next.row +1){
current.topWall = false
next.bottomWall = false
}
if (current.col == next.col && current.row == next.row -1){
current.bottomWall = false
next.topWall = false
}
if (current.col == next.col + 1 && current.row == next.row){
current.leftWall = false
next.rightWall = false
}
if (current.col == next.col - 1 && current.row == next.row +1){
current.rightWall = false
next.leftWall = false
}
}
var cells: Array<Array<Cell>> = Array(COLS, {Array(ROWS, {Cell()})})
fun createMaze(){
var stack = Stack<Cell>()
var current:Cell
var next:Cell
for(x in 0 until COLS){
for(y in 0 until ROWS){
cells[x][y] = Cell(x,y)
}
}
current = cells[0][0]
current.visited = true
do{
next = getNeighbour(current)
removeWall(current,next)
stack.push(current)
current = next
current.visited = true
}while (!stack.empty())
}
override fun onDraw(canvas: Canvas) {
canvas.drawColor(Color.DKGRAY)
val ancho = width
val alto = height
println(ancho)
println(alto)
if(ancho/alto < COLS/ROWS){
cellSize = ancho/(COLS+1).toFloat()
}else{
cellSize = alto/(ROWS+1).toFloat()
}
hMargin = (ancho - COLS*cellSize)/2
vMargin = ((alto - ROWS*cellSize)/2)
canvas.translate(hMargin,vMargin)
for(x in 0 until COLS){
for(y in 0 until ROWS){
if (cells[x][y].topWall){
canvas.drawLine(
x*cellSize,
y*cellSize,
(x+1)*cellSize,
y*cellSize,
wallPaint)
}
if (cells[x][y].leftWall){
canvas.drawLine(
x*cellSize,
y*cellSize,
x*cellSize,
(y+1)*cellSize,
wallPaint)
}
if (cells[x][y].bottomWall){
canvas.drawLine(
x*cellSize,
(y+1)*cellSize,
(x+1)*cellSize,
(y+1)*cellSize,
wallPaint)
}
if (cells[x][y].rightWall){
canvas.drawLine(
(x+1)*cellSize,
y*cellSize,
(x+1)*cellSize,
(y+1)*cellSize,
wallPaint)
}
}
}
}
}
class Cell(var col:Int = 0, var row: Int = 0 ){
var topWall = true
var leftWall = true
var bottomWall = true
var rightWall = true
var visited = false
}
您方法的 return 类型不允许 null
被 returned。您需要将其更改为可空 Cell?
(可空性由问号表示)。
fun getNeighbour(cell:Cell): Cell? {
var vecinos: ArrayList<Cell> = ArrayList()
.....
.....
从语法上看
// first change
fun getNeighbour(cell:Cell): Cell -> fun getNeighbour(cell:Cell): Cell?
// second change
var next:Cell -> var next:Cell? = null
// third change
next = getNeighbour(current)
if(next != null) {
// you process code goes here
}
但是从设计的角度来说,fun getNeighbour(cell:Cell): Cell
意味着总能找到邻居。
如果不是这种情况,您应该将其更改为 Cell?
并且以下代码应处理 null 情况。