N
N
NameOf Var2017-08-23 23:59:34
Java
NameOf Var, 2017-08-23 23:59:34

How to pass object (Parcelable) between Activity?

I have a NewsRequest object that I want to pass to the second Activity. Here is his announcement:

public class NewsRequest implements Parcelable {

    private boolean success;

    private List<News> newsList;

    protected NewsRequest(Parcel in) {
        success = in.readByte() != 0;
        in.readList(newsList, News.class.getClassLoader());
    }

    public static final Creator<NewsRequest> CREATOR = new Creator<NewsRequest>() {
        @Override
        public NewsRequest createFromParcel(Parcel in) {
            return new NewsRequest(in);
        }

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

    public List<News> getNewsList() {
        return newsList;
    }

    public boolean isSuccess() {
        return success;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeByte((byte) (success ? 1 : 0));
        parcel.writeTypedList(newsList);
    }
}

news:
public class News implements Parcelable {

    private long date;

    private String sourceLogo;

    private String title;

    private String url;

    private String source;

    protected News(Parcel in) {
        date = in.readLong();
        sourceLogo = in.readString();
        title = in.readString();
        url = in.readString();
        source = in.readString();
    }

    public static final Creator<News> CREATOR = new Creator<News>() {
        @Override
        public News createFromParcel(Parcel in) {
            return new News(in);
        }

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

    public long getDate() {
        return date;
    }

    public String getSourceLogo() {
        return sourceLogo;
    }

    public String getTitle() {
        return title;
    }

    public String getUrl() {
        return url;
    }

    public String getSource() {
        return source;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(date);
        parcel.writeString(sourceLogo);
        parcel.writeString(title);
        parcel.writeString(url);
        parcel.writeString(source);
    }
}

This is how I pass the object:
Intent intent = new Intent(StartActivity.this, NewsActivity.class);
        intent.putExtra(NewsRequest.class.getCanonicalName(), news);
        startActivity(intent);

And this is how I take it:
NewsRequest getNews = (NewsRequest) getIntent().getParcelableExtra(
                NewsRequest.class.getCanonicalName());

The program crashes on the line (NullPointerException): in.readList(newsList, News.class.getClassLoader()); (NewsRequest.java)
Tried also in.readList(newsList, null) - same error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-08-24
@hax

The 21st century is in the yard, I have already begun to forget how to implement parselabl. Use AutoValue.
According to NPE, there are no real out-parameters in Java. You pass a list to the method and it is filled in the method. You pass null and NPE happens. Initialize the list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question