Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.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
Java 包裹位图-Android_Java_Android - Fatal编程技术网

Java 包裹位图-Android

Java 包裹位图-Android,java,android,Java,Android,在现有代码中,我有以下类来保存类别详细信息 import org.json.JSONObject; import android.graphics.Bitmap; import android.os.Parcel; import android.os.Parcelable; public class Category { private String mIconURL; private String mName; public Category() { super(); }

在现有代码中,我有以下类来保存类别详细信息

import org.json.JSONObject;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

public class Category  {


private String mIconURL;
private String mName;



public Category() {
    super();
}

public String getIconURL() {
    return mIconURL;
}


public void setIconURL(String iconURL) {
    this.mIconURL = iconURL;
}

public String getName() {
    return mName;
}

public void setName(String name) {
    this.mName = name;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    super.writeToParcel(dest, flags);

    dest.writeString(mIconURL);
    dest.writeString(mName);


}

public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {

    public Category createFromParcel(Parcel in) {
        return new Category(in);
    }

    public Category[] newArray(int size) {
        return new Category[size];
    }
};

private Category(Parcel in) {
    super(in);
    mIconURL = in.readString();
    mName = in.readString();


}

@Override
public int describeContents() {

    return 1;
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("{");
    builder.append(super.toString());
    builder.append(", mName=").append(mName);
    builder.append(", mIconURL=").append(mIconURL);
    builder.append("}");
    return builder.toString();
}

public static Category parseFromJSON(JSONObject jsonObject) {
    Category cat = new Category();
    try {
        cat.setServerId(jsonObject.getInt("id"));
        cat.setName(jsonObject.getString("name"));
        cat.setIconURL(jsonObject.getString("icon"));

    } catch (Exception e) {

    }
    return cat;
}
请告诉我你有一个例子


即使
mImage==null

尝试一下也可以

public class MyVeryOwnParcelable  implements Parcelable {

//you can use every type of data even Arraylist

//change here (1)
public String ID;
public Bitmap BITMAP_IMAGE;

public MyVeryOwnParcelable  () {

}
public MyVeryOwnParcelable  (Parcel parcel) {
    //change here (2)
    this.ID= parcel.readString();
    this.BITMAP_IMAGE=parcel.readParcelable(null);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    //change here (3)
    dest.writeString(ID);
    dest.writeParcelable(BITMAP_IMAGE, flags);
}

@Override
public int describeContents() {
    return 0;
}
// Method to recreate a Catch from a Parcel
public static Creator<MyVeryOwnParcelable  > CREATOR = new Creator<MyVeryOwnParcelable  >() {

    @Override
    public MyVeryOwnParcelable  createFromParcel(Parcel source) {
        return new MyVeryOwnParcelable  (source);
    }

    @Override
    public MyVeryOwnParcelable  [] newArray(int size) {
        return new MyVeryOwnParcelable  [size];
    }

  };
}
MyVeryOwnParcelable mp=new MyVeryOwnParcelable();
mp.ID="hello parcel";
mp.BITMAP_IMAGE=BitmapFactory.decodeResource(getResources(), R.drawable.myimage);

//put as normal
intent.putExtra("MY_DATA",mp);

//get from intent
MyVeryOwnParcelable  my= (MyVeryOwnParcelable )   getIntent().getExtras().getParcelable(KEY.EXTRA_DATA);
//do anything with data

imageView.setImageBitmap(mp.BITMAP_IMAGE);
只需根据您的需要更改“MyVeryOwnParcelable”,我已经指出了需要更改的地方


希望这将有助于某人

和在接收意图上,
packet.setDataPosition(0);destinationBitmap=Bitmap.CREATOR.createFromParcel(地块)03-06 01:17:53.643:E/包裹(15238):解组时未找到类别:��an 03-06 01:17:53.643:E/包裹(15238):java.lang.ClassNotFoundException:��an 03-06 01:17:53.643:E/Parcel(15238):at java.lang.Class.classForName(本机方法)03-06 01:17:53.643:E/Parcel(15238):at java.lang.Class.forName(Class.java:251)03-06 01:17:53.643:E/Parcel(15238):使用
writeParcelable()
而不是
writeValue()
public void writeToParcel(Parcel dest, int flags) {
...
    dest.writeValue(mImage);
}
private Category(Parcel in) {
...
    mImage= in.readParcelable(Bitmap.class.getClassLoader());
}
public class MyVeryOwnParcelable  implements Parcelable {

//you can use every type of data even Arraylist

//change here (1)
public String ID;
public Bitmap BITMAP_IMAGE;

public MyVeryOwnParcelable  () {

}
public MyVeryOwnParcelable  (Parcel parcel) {
    //change here (2)
    this.ID= parcel.readString();
    this.BITMAP_IMAGE=parcel.readParcelable(null);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    //change here (3)
    dest.writeString(ID);
    dest.writeParcelable(BITMAP_IMAGE, flags);
}

@Override
public int describeContents() {
    return 0;
}
// Method to recreate a Catch from a Parcel
public static Creator<MyVeryOwnParcelable  > CREATOR = new Creator<MyVeryOwnParcelable  >() {

    @Override
    public MyVeryOwnParcelable  createFromParcel(Parcel source) {
        return new MyVeryOwnParcelable  (source);
    }

    @Override
    public MyVeryOwnParcelable  [] newArray(int size) {
        return new MyVeryOwnParcelable  [size];
    }

  };
}
MyVeryOwnParcelable mp=new MyVeryOwnParcelable();
mp.ID="hello parcel";
mp.BITMAP_IMAGE=BitmapFactory.decodeResource(getResources(), R.drawable.myimage);

//put as normal
intent.putExtra("MY_DATA",mp);

//get from intent
MyVeryOwnParcelable  my= (MyVeryOwnParcelable )   getIntent().getExtras().getParcelable(KEY.EXTRA_DATA);
//do anything with data

imageView.setImageBitmap(mp.BITMAP_IMAGE);