简体   繁体   English

为什么解码字节数组给我一个空指针错误

[英]Why is decodeByteArray giving me a null pointer error

I'm trying to pass a byte array from an image to an activity through a bundle 我正在尝试通过捆绑包将图像的字节数组传递给活动

creating the byte array with 用创建字节数组

            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream picArray = new ByteArrayOutputStream(); 
            thumbnail.compress(CompressFormat.PNG, 0, picArray); 
            byte[] picData = picArray.toByteArray();

when i try to decode the byte array into a bitmap i get a null pointer exception which FC's (running on physical phone) 当我尝试将字节数组解码为位图时,我得到了FC的空指针异常(在物理电话上运行)

I've been going through line by line and this is the only thing creating an error 我一直在逐行检查,这是唯一会造成错误的事情

            //grab byte array taken
            Intent sent = this.getIntent();
            Bundle arrayForPic = sent.getExtras();
            byte[] picArray = arrayForPic.getByteArray("pictureArray");

            //convert array into bitmap
            //null error happens HERE
            Bitmap thumbnail = BitmapFactory.
               decodeByteArray(picArray, 0, picArray.length);

the errors are 错误是

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.HandlePic}: java.lang.NullPointerException


Caused by: java.lang.NullPointerException
at com.test.HandlePic.onCreate(HandlePic.java:43)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1703)

line 43 is where the decodeByteArray is 第43行是decodeByteArray所在的位置

I would guess that picArray is null. 我猜想picArray为空。 Presumably because there is no pictureArray value in the bundle. 大概是因为捆绑包中没有pictureArray值。 You should be able to step through the code with a debugger to see exactly what is going wrong. 您应该能够使用调试器逐步遍历代码,以查看到底出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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