0

I have a class which is a subclass of ArrayAdpater, and I'm trying to make it parcable.

I keep getting this error
"Error:(21, 36) error: Parceler: Unable to find read/write generator for type android.content.Context for android.content.Context context"

Here is the class:

    @org.parceler.Parcel
    public class conversation_pager extends ArrayAdapter<String> {
    private final ArrayList<String> messages;
    private Context context;

    @ParcelConstructor
    public conversation_pager(Context context) {
            super(context, -1);
            // Initilize our variables
            this.context = context;
            this.messages = null;
    }

    public void addMessage(String user, String message) {
            // Calm the linter down about a NUllPointerException.
            if (messages == null) {
                    return;
            }
            // Add the message.
            messages.add("<%s> %s".format(user, message));
            notifyDataSetChanged();
    }
};

I'm trying to avoid using a static context variable.

1 Answers1

0

You shouldn't pass the context via a Parcelable. You'll need to rework your @Parcel object to only hold data and pass in the context where needed. Possibly like the following?:

public class ConversationPager extends ArrayAdapter<String> {
    private final List<String> messages;
    private fianl Context context;

    public ConversationPager(Context context, List<Messages> messages) {
         super(context, -1);
            // Initilize our variables
            this.context = context;
            this.messsages = messages;
    }

    public ConversationPager(Context contex) {
           this(context, new ArrayList<String>());
    }

    public void addMessage(String user, String message) {
            // Calm the linter down about a NUllPointerException.
            if (messages == null) {
                    return;
            }
            // Add the message.
            messages.add("<%s> %s".format(user, message));
            notifyDataSetChanged();
    }

    public List<String> getMessages() {
        return messages;
    }
};

Then you can wrap/unwrap the messages directly:

ConversationPager pager = new ConversationPager(context);

// Add some messages

// Parcel messages directly 
Parcels.wrap(pager.getMessages();

// Then unwrap in the receiving context:
List<String> messages = Parcels.unwrap(incomingParcelableExtra);
ConversationPager 

pager = new ConversationPager(context, messages);
John Ericksen
  • 10,995
  • 4
  • 45
  • 75
  • That means you're still trying to include the Context object in a generated Parcelable. Did you annotate Conversation over? Notice I removed that annotation in my example. – John Ericksen May 08 '17 at 13:53