android - 如何使用 Kotlin 修复 Null 不能成为 Android 迷宫游戏中非空类型的值

标签 android class kotlin maze

我正在按照本教程在 android 中创建一个迷宫,但是当我在 getNeighbour 方法中返回 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

}

最佳答案

从语法上看

// 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 - 如何使用 Kotlin 修复 Null 不能成为 Android 迷宫游戏中非空类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56487595/

相关文章:

java - 线程 "main"java.lang.IllegalAccessError : failed to access class 中出现异常

java - 将 Java 函数传递给 KFunction

Kotlin For 循环从给定的索引开始

android - 没有数据时如何为应用程序小部件设置默认文本?

android - 如何使HTML5输入类型“日期”在Android上触发 native 日期选择器?

java - onCreateView fragment 未调用

android - Kotlin 检查变量是否在两个数字之间

Android WiFi Direct 演示问题

python - 继承后检索类变量

java - 如何使用 LinkedList 作为实例字段来管理不可变类?