简体   繁体   中英

How can I solve "NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap"?

I have been trying to follow this tutorial: https://developer.android.com/training/camera/photobasics
I use an imagebutton, so I open the camera and take the photo, but when I accept the picture it just crashes:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bujess, PID: 15270
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.bujess/com.example.bujess.WritingActivity}: java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5097)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5138)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7814)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
     Caused by: java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap
        at com.example.bujess.WritingActivity.onActivityResult(WritingActivity.kt:61)
        at android.app.Activity.dispatchActivityResult(Activity.java:8292)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5090)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5138) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7814) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068) 

The most suspicious line is "at com.example.bujess.WritingActivity.onActivityResult(WritingActivity.kt:61)". But those are the lines I copied from the guide onActivityResult:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            val imageBitmap = data?.extras?.get("data") as Bitmap
            val imageView = findViewById<ImageView>(R.id.opGallNCam)
            imageView?.setImageBitmap(imageBitmap)
            galleryAddPic()
        }
    }

Line 61 being : "val imageBitmap = data?.extras?.get("data") as Bitmap"
opGallNCam is the Id of the ImageButton I am new to Kotlin. I thank you in advance

java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.Bitmap

The problem is that: data?.extras?.get("data") returns a null value, and you are trying to cast this null into a Bitmap

First data?.extras?.get("data") won't return you a Bitmap, but you can get a Uri object first with data?.data , and then convert it into a Bitmap as illustrated below, change your onActivityResult method to:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 10 && resultCode == RESULT_OK) {
        val imageUri = data?.data as Uri
        var imageBitmap: Bitmap
        
        val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            imageBitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(contentResolver, imageUri))
        } else {
            imageBitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
        }
        
//............... rest of your code
        

Source of conversion

To avoid an exception being thrown, one can use a safe cast operator as? that returns null on failure

val imageBitmap = data?.extras?.get("data") as? Bitmap

Have a look at this you will know more about it https://kotlinlang.org/docs/reference/typecasts.html#safe-nullable-cast-operator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM