android.os.Parcel.readByteArray NullPointerException
来源:互联网 发布:剑灵论坛捏脸数据 编辑:程序博客网 时间:2024/06/06 12:57
报错信息:
E/AndroidRuntime( 1626): java.lang.NullPointerExceptionE/AndroidRuntime( 1626): at android.os.Parcel.readByteArray(Parcel.java:1649)E/AndroidRuntime( 1626): at xxxx.b.a(ContactInfo.java:47)E/AndroidRuntime( 1626): a xxxxxx.b.createFromParcel(ContactInfo.java:40)E/AndroidRuntime( 1626): at android.os.Parcel.readParcelable(Parcel.java:2111)E/AndroidRuntime( 1626): at android.os.Parcel.readValue(Parcel.java:2020)E/AndroidRuntime( 1626): at android.os.Parcel.readListInternal(Parcel.java:2362)E/AndroidRuntime( 1626): at android.os.Parcel.readList(Parcel.java:1585)E/AndroidRuntime( 1626): at xxx.util.m.b(ParcelableUtils.java:54)E/AndroidRuntime( 1626): atxxxxxx.xxx.onClick(MainActivity.java:646)E/AndroidRuntime( 1626): at android.view.View.performClick(View.java:4444)E/AndroidRuntime( 1626): at android.view.View$PerformClick.run(View.java:18445)E/AndroidRuntime( 1626): at android.os.Handler.handleCallback(Handler.java:733)E/AndroidRuntime( 1626): at android.os.Handler.dispatchMessage(Handler.java:95)E/AndroidRuntime( 1626): at android.os.Looper.loop(Looper.java:136)E/AndroidRuntime( 1626): at android.app.ActivityThread.main(ActivityThread.java:5135)E/AndroidRuntime( 1626): at java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime( 1626): at java.lang.reflect.Method.invoke(Method.java:515)E/AndroidRuntime( 1626): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)E/AndroidRuntime( 1626): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)E/AndroidRuntime( 1626): at dalvik.system.NativeStart.main(Native Method)W/ActivityManager( 789): Force finishing activity xxxxx/.MainActivity
问题说明
当我在通过Parcel传递byte[]时出现了这个错误
public class ContactInfo implements Parcelable { private String mName;//联系人名称 private String mNumber;// 联系人号码// private static byte[] bmBytes;//联系人头像 private Bitmap bitmap;//头像bitmap @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(mName); dest.writeString(mNumber); byte[] bytes=getBytesFromBitmap(bitmap); dest.writeByteArray(bytes); } // 用来创建自定义的Parcelable的对象 public static final Creator<ContactInfo> CREATOR = new Creator<ContactInfo>() { public ContactInfo createFromParcel(Parcel parcel) { ContactInfo info = new ContactInfo(); String name = parcel.readString(); info.setmName(name); String number = parcel.readString(); info.setmNumber(number); byte[] bytes; parcel.readByteArray(bytes); info.setBitmap(getBitmapFromBytes(bytes)); return info; } public ContactInfo[] newArray(int size) { return new ContactInfo[size]; } }; public static Bitmap getBitmapFromBytes(byte[] bytes) { return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); } private byte[] getBytesFromBitmap(Bitmap bitmap) { ByteArrayOutputStream bytesOS = new ByteArrayOutputStream(); //this.bitmap.compress(Bitmap.CompressFormat.PNG, 0, bytesOS); bitmap.compress(Bitmap.CompressFormat.PNG, 0, bytesOS); return bytesOS.toByteArray(); } public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; } public Bitmap getBitmap() { return bitmap; } public String getmName() { return mName; } public void setmName(String mName) { this.mName = mName; } public String getmNumber() { return mNumber; } public void setmNumber(String mNumber) { this.mNumber = mNumber; }}
解决
需要修改为在写入时使用:
byte[] bytes=getBytesFromBitmap(bitmap);dest.writeInt(bytes.length);dest.writeByteArray(bytes);
在读取时使用:
byte[] bytes=new byte[parcel.readInt()];parcel.readByteArray(bytes);
阅读全文
0 0
- android.os.Parcel.readByteArray NullPointerException
- Parcel android.os.Parcel@439e4cf0
- android.os.TransactionTooLargeException: data parcel size 962920 bytes
- android.os.NetworkOnMainThreadException 和 java.lang.nullpointerexception
- Android Parcel
- Android Parcel
- android parcel
- Android Parcel
- android parcel
- 安卓bundle传递序列化对象RuntimeException: Parcel android.os.Parcel@8325970: Unmarshalling unknown type code
- Android调用系统裁减图片,出现android.os.TransactionTooLargeException: data parcel size 642356 bytes
- java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 520536 bytes
- android 中Parcel
- Android Parcel理解
- Android Parcel理解
- Android中的Parcel是什么
- 【引】android Parcelable Parcel
- Android Parcel学习
- Maven学习,解析,Maven坐标简介
- Python中列表的使用总结
- 图像标注工具labelImg安装方法(win10+Python3.5)
- mongodb主从复制
- Java并发小结(在公司遇到并发下问题的总结--1)
- android.os.Parcel.readByteArray NullPointerException
- 集合框架(产生10个1-20之间的随机数要求随机数不能重复)
- 2017-06-12 每日一记 Linux的root密码修改
- ARKit & OpenGL ES
- 浅谈HTTP中Get、Post、Put与Delete的区别
- Java实现-将整数A转换为B
- Spring AOP
- 数组按照对象属性排序
- Python3中raw_input() was renamed to input()