胖蔡说技术
随便扯扯

android.os.Parcel.readByteArray NullPointerException

E/AndroidRuntime( 1626): java.lang.NullPointerException
E/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) 打赏
转载请附上原文出处链接:胖蔡叨叨叨 » android.os.Parcel.readByteArray NullPointerException
分享到: 更多 (0)

请小编喝杯咖啡~

支付宝扫一扫打赏

微信扫一扫打赏