Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 智能转换到';位图';这是不可能的,因为';位图1';是此时可能已更改的可变属性 bitmap1=Bitmap.createScaledBitmap( bitmap1,//bitmap1=Bitmap.createScaledBitmap( bitmap1!!,/!!_Kotlin - Fatal编程技术网

Kotlin 智能转换到';位图';这是不可能的,因为';位图1';是此时可能已更改的可变属性 bitmap1=Bitmap.createScaledBitmap( bitmap1,//bitmap1=Bitmap.createScaledBitmap( bitmap1!!,/!!

Kotlin 智能转换到';位图';这是不可能的,因为';位图1';是此时可能已更改的可变属性 bitmap1=Bitmap.createScaledBitmap( bitmap1,//bitmap1=Bitmap.createScaledBitmap( bitmap1!!,/!!,kotlin,Kotlin,是的,这是真的:)不能像这样使用值,因为它在某个点上可以为null createScaledBitmap需要不可为空的Bitmap,但不能保证您使用的位图在调用给定函数时不会为空 那么,你能做什么? 调用前,检查位图是否为空: bitmap1 = Bitmap.createScaledBitmap( bitmap1!!, // !! <--- helps (width.toInt()), (height.toInt()), false) n

是的,这是真的:)不能像这样使用值,因为它在某个点上可以为null

createScaledBitmap
需要不可为空的
Bitmap
,但不能保证您使用的位图在调用给定函数时不会为空

那么,你能做什么? 调用前,检查位图是否为空:

    bitmap1 = Bitmap.createScaledBitmap(

    bitmap1!!, // !! <--- helps

    (width.toInt()),

    (height.toInt()),

    false)

numberOfInvaders ++



            if (uhOrOh) {
                canvas.drawBitmap(Invader.bitmap1!!, //  here 
                    invader.position.left,
                    invader.position.top,
                    paint)
            } else {
                canvas.drawBitmap(Invader.bitmap2!!, //  and here too
                    invader.position.left,
                    invader.position.top,
                    paint)
            }
在多线程环境中,存在一种风险,即在执行代码块的过程中,值无论如何都会发生变化,因此您可以将
let
函数与
运算符一起使用。
运算符(基本上与
相同,但仅在值不为null时执行)。将使用一个有效的最终参数调用块代码,该参数是用于调用此方法的实例,在本例中为“位图”,称为“上下文对象”,可通过
it
关键字访问:

if (bitmap != null) { /* code here, still requires !! operator */ }
还有另一种方法是使用
!!
运算符(但如果值为null,则它可以以NPE异常结束)。仅当您确定此时此值不会为null时才使用,否则您可能会使应用程序崩溃

此外,您还可以使用
?:
运算符-如果不为null,则取第一个值,否则取第二个值。这很好,因为您可以使用默认值。此外,您还可以在那里引发异常;)


在这种情况下,您将得到异常,但带有非常交流的消息(而不仅仅是NPE)。

它需要一个非空位图(所以是
位图
),但是
位图1
是可空的(所以是
位图?
),所以我必须做些什么来修复它?我有点新发现了,只是需要添加!!它的旁边是
?。let
并不能使智能强制转换成为可能,但它提供了一个不可为空的值来代替可为空的值。
    bitmap1 = Bitmap.createScaledBitmap(

    bitmap1!!, // !! <--- helps

    (width.toInt()),

    (height.toInt()),

    false)

numberOfInvaders ++



            if (uhOrOh) {
                canvas.drawBitmap(Invader.bitmap1!!, //  here 
                    invader.position.left,
                    invader.position.top,
                    paint)
            } else {
                canvas.drawBitmap(Invader.bitmap2!!, //  and here too
                    invader.position.left,
                    invader.position.top,
                    paint)
            }
if (bitmap != null) { /* code here, still requires !! operator */ }
bitmap?.let { /* code here, bitmap is passed as effectively final, so for sure it's not null  */ }
bitmap ?: throw IllegalStateException("bitmap is null") // exception
bitmap ?: DEFAULT_BITMAP // default bitmap, if any