2

I have the following in a class that is implementing Parcelable:

private static final long serialVersionUID = 66;
private HashMap<Integer, Bitmap> mBitmaps;
private HashMap<Integer, Drawable> mDrawables;
private Context mContext;

And:

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeValue(mBitmaps);
    dest.writeValue(mDrawables);
    dest.writeValue(mContext);
    dest.writeByte((byte) (mActive ? 0x01 : 0x00));

}

I get the error:

java.lang.RuntimeException: Parcel: unable to marshal value android.app.Application@4051cfe0
at android.os.Parcel.writeValue(Parcel.java:1132)
at com.example.example.ImageManager.writeToParcel(ImageManager.java:82)


dest.writeValue(mContext); is on line 82.
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
odds0cks
  • 141
  • 4
  • 11
  • 1
    Big question is why do you need to write context into parcel??? And small answer is Context is not parcelable so you can not. – Pankaj Kumar Apr 11 '14 at 06:52
  • try read this http://stackoverflow.com/questions/3818745/androidruntime-error-parcel-unable-to-marshal-value – xoxol_89 Apr 11 '14 at 06:57
  • In my code I have: BitmapFactory.decodeResource(mContext.getResources(), resource)); Can I change that so I don't have to use a context to access getResources() ? – odds0cks Apr 11 '14 at 06:58

3 Answers3

0

As stated in this answer: https://stackoverflow.com/a/6112919/7721253

You are trying to add to the parcel objects which are not serializable. Objects within the objects you are serializing have to be serializable as well.

jpalvarezl
  • 325
  • 2
  • 8
-1

You are writing to Parcel.

Did you implemeted Parcelable to do so ?

You need to implement the Serializable or Parcelable interface. Otherwise the value will not be marsheled as the error suggests.

Also,

Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface.

Check about android Parcelable.

sjain
  • 23,126
  • 28
  • 107
  • 185
-1

Just write a little Class thats holding your Context-Object like this:

import android.content.Context;

public class ContextHolder {
    public static Context context = null;

    public static void setContext(Context context){
        ContextHolder.context = context;
    }

    public static Context getContext(){
        return context;
    }
}

then whenever you need the Context, you can just call the ContextHolder.getContext() function from everywhere you need it.

Cheers

Malte87
  • 7
  • 1