I am using Parcelable for a custom Song object I have in my class. However, when ever I am trying to get an arraylist extra of type Song I always get a null pointer exception. I have no problem getting string extras from the intent but when I try to get this ArrayList I need I always get null. I am also getting a null when I just try to pass a Song object, so I am assuming there is some issue with it but I cannot figure it out for the life of me.
This is my song class
import android.os.Parcel;
import android.os.Parcelable;
public class Song implements Parcelable {
private String uri;
private String title;
private String artist;
private String album;
private String length;
private int count;
private int source;
public Song () {
}
public Song (String title, String artist, String album, String uri, String length, int count,
int source) {
this.uri = uri;
this.title = title;
this.artist = artist;
this.album = album;
this.length = length;
this.count = count;
this.source = source;
}
public String getUri() {
return uri;
}
public String getArtist() {
return artist;
}
public String getTitle() {
return title;
}
public String getLength() {
return length;
}
public int getCount() {return count;}
#Override
public String toString() {
return title + " - " +artist;
}
#Override
public boolean equals(Object o){
if (o instanceof Song) {
Song song = (Song) o;
if (title.equals(song.title) && length.equals(song.length)) {
return true;
}
}
return false;
}
public int compareTo(Song s) {
if (this.count < s.getCount()) {
return -1;
}
else if(this.count > s.getCount()){
return 1;
}
return 0;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public int getSource() {
return source;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.uri);
dest.writeString(this.title);
dest.writeString(this.artist);
dest.writeString(this.album);
dest.writeString(this.length);
dest.writeInt(this.count);
dest.writeInt(this.source);
}
protected Song(Parcel in) {
this.uri = in.readString();
this.title = in.readString();
this.artist = in.readString();
this.album = in.readString();
this.length = in.readString();
this.count = in.readInt();
this.source = in.readInt();
}
public static final Creator<Song> CREATOR = new Creator<Song>() {
#Override
public Song createFromParcel(Parcel source) {
return new Song(source);
}
#Override
public Song[] newArray(int size) {
return new Song[size];
}
};
}
This is the line I use to package the arraylist
Intent intent = new Intent();
Log.d("UTILS ", " size: " +queue.size()); // Making sure it is not null before passing
intent.setClass(context, PlayerService.class);
intent.putParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST, queue);
context.startService(intent);
This is retrieving the arraylist in PlayerService class
public static final String EXTRA_TRACK_LIST = "EXTRA_TRACK_LIST";
private ArrayList<Song> trackList;
.
.
.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null || intent.getAction() == null) {
Log.d(TAG, "unspecified command");
return START_STICKY;
}
trackList=intent.getParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST);
if (trackList == null) {
Log.d("TRACKLIST", "IS NULL");
} else {
Log.d(TAG, "size: "+trackList.size());
}
.
.
.
// more irrelevant code
your parameter is wrong
public static final String EXTRA_TRACK_LIST = "EXTRA_TRACK_LIST";
so change like this
trackList = intent.getParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST);
Is queue a Queue/List or similar? Or is it an array? If array, should be queue.length, right? From List to array try this. This should work.
intent.putParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST, queue);
If not, let me know to keep trying. Also, you could also pass it as String. You could use GSON to convert it to JSON string. After converted to String, pass it as a single String.
Intent intent = new Intent(ActivityA.this, Service.class);
intent.putExtra(new Gson().toJson(queue), PlayerService.EXTRA_TRACK_LIST);
context.startService(intent);
And to retrieve that object again:
Song song = new Gson().fromJson(intent.getStringExtra(PlayerService.EXTRA_TRACK_LIST), Song.class);
Or, if list (I believe you case):
Type listType = new TypeToken<ArrayList<Song.class>>(){}.getType();
List<Song> songList = new Gson().fromJson(intent.getStringExtra(PlayerService.EXTRA_TRACK_LIST), listType);
Related
I'm trying to put a Parcelable object as an extra in an intent and pass it to the next Activity, and it doesn't crash but the object changes dramatically. I'm sending when clicking on an item from a RecyclerView in a Fragment and opening an Activity from it.
This is how I send it:
AdminProfile adminProfile = list.get(position).admin;
Intent intent = new Intent(view.getContext(),ClosedChatActivity.class);
intent.putExtra("chat",adminProfile);
view.getContext().startActivity(intent);
This how I get it:
adminProfile = (AdminProfile) getIntent().getExtras().getParcelable("chat");
And here the class:
public class AdminProfile implements Parcelable {
public static final Creator<AdminProfile> CREATOR = new Creator<AdminProfile>() {
#Override
public AdminProfile createFromParcel(Parcel in) {
return new AdminProfile(in);
}
#Override
public AdminProfile[] newArray(int size) {
return new AdminProfile[size];
}
};
public Long idUser;
public String name;
public String professio;
public String description;
public List<WebLink> webLinks;
public Long idOficina;
protected AdminProfile(Parcel in) {
if (in.readByte() == 0) {
idUser = null;
} else {
idUser = in.readLong();
}
name = in.readString();
professio = in.readString();
description = in.readString();
webLinks = in.createTypedArrayList(WebLink.CREATOR);
if (in.readByte() == 0) {
idOficina = null;
} else {
idOficina = in.readLong();
}
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(idUser);
parcel.writeString(name);
parcel.writeString(professio);
parcel.writeString(description);
parcel.writeLong(idOficina);
parcel.writeList(webLinks);
}
}
I can't understand why, but when I send the object I have UserId=3, but when I get it it's userId=55834574848. Any ideas?
The Parcelable functions were filled automatically by Android Studio, and reading the first byte messed it up.
Changing
if (in.readByte() == 0) {
idUser = null;
} else {
idUser = in.readLong();
}
for
idUser = in.readLong();
fixed it.
I have 2 model classes(Data,Title) which contain the same field:
String dataID. I want to get both of this IDs with interface implementation.
I am passing Title model through Bundle to another Activity, passing Data model through Bundle in that same activity(just creating new instance of the activity and resetting information).
I want both of my model classes to implement SharedID interface, with method String getSharedId();
How can I get different ids but from different models? I need to put only one parameter and it should be String in my ViewModelFactory constructor.
public class Data implements SharedId,Parcelable {
private String text;
private String textHeader;
private int viewType;
private String mainId;
private String dataID;
public Data() { }
public String getDataID() {
return dataID;
}
public void setDataID(String dataID) {
this.dataID = dataID;
}
public String getText() {return (String) trimTrailingWhitespace(text); }
public void setText(String text) {
this.text = (String) trimTrailingWhitespace(text);
}
public String getTextHeader() {
return (String) trimTrailingWhitespace(textHeader);
}
public void setTextHeader(String textHeader) {
this.textHeader = textHeader;
}
public int getViewType() {
return viewType;
}
public void setViewType(int viewType) {
this.viewType = viewType;
}
public String getMainId() {
return mainId;
}
public void setMainId(String mainId) {
this.mainId = mainId;
}
protected Data(Parcel in) {
text = in.readString();
textHeader = in.readString();
viewType = in.readInt();
mainId = in.readString();
dataID = in.readString();
}
#Override
public String toString() {
return "Data{" +
"order=" +
", text='" + text + '\'' +
", textHeader='" + textHeader + '\'' +
", viewType=" + viewType +
'}';
}
#SuppressWarnings("StatementWithEmptyBody")
public static CharSequence trimTrailingWhitespace(CharSequence source) {
if (source == null) {
return "";
}
int i = source.length();
// loop back to the first non-whitespace character
while (--i >= 0 && Character.isWhitespace(source.charAt(i))) {
}
return source.subSequence(0, i + 1);
}
public static final Creator<Data> CREATOR = new Creator<Data>() {
#Override
public Data createFromParcel(Parcel in) {
return new Data(in);
}
#Override
public Data[] newArray(int size) {
return new Data[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(text);
dest.writeString(textHeader);
dest.writeInt(viewType);
dest.writeString(mainId);
dest.writeString(dataID);
}
#Override
public String getSharedDataId() {
return getDataID();
}
}
public class Title implements SharedId,Parcelable {
private String dataID;
private String title;
public Title() { }
protected Title(Parcel in) {
dataID = in.readString();
title = in.readString();
}
public String getDataID() {
return dataID;
}
public void setDataID(String dataID) {
this.dataID = dataID;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public static final Creator<Title> CREATOR = new Creator<Title>() {
#Override
public Title createFromParcel(Parcel in) {
return new Title(in);
}
#Override
public Title[] newArray(int size) {
return new Title[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(dataID);
dest.writeString(title);
}
#NonNull
#Override
public String toString() {
return "Title{" +
"dataID='" + dataID + '\'' +
", titleOrder=" +
", title='" + title + '\'' +
'}';
}
#Override
public String getSharedDataId() {
return getDataID();
}
}
And My DetailActivity code, I already succeeded with the mission of passing id, but i need to do this trough interfaces :( So help me out friends, would really appreciate it!
public class DetailActivity extends AppCompatActivity implements
DetailAdapter.OnDialogClickListener,
DetailAdapter.OnDetailClickListener {
private static String id;
private String parentId;
private Data data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
TextView tvToolbarTitle = findViewById(R.id.title_toolbar_detail);
tvToolbarTitle.setSelected(true);
findViewById(R.id.btn_back).setOnClickListener(v -> finish());
ArrayList<SharedId> sharedIds = new ArrayList<>();
sharedIds.add(new Title());
sharedIds.add(new Data());
for (SharedId sharedId : sharedIds){
System.out.println(sharedId.getSharedDataId());
}
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Title model = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
Data childModel = bundle.containsKey("idDetail") ? bundle.getParcelable("idDetail") : null;
}
if (bundle != null) {
Title model = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
Data childModel = bundle.containsKey("idDetail") ? bundle.getParcelable("idDetail") : null;
String parentId = bundle.getString("mainScreenId");
if (parentId != null) {
this.parentId = parentId;
}
if (model != null) {
this.id = model.getDataID();
tvToolbarTitle.setText(model.getTitle());
}
if (childModel != null) {
this.id = childModel.getDataID();
tvToolbarTitle.setText(childModel.getTextHeader());
}
}
RecyclerView recyclerView = findViewById(R.id.rv_detail);
DetailAdapter adapter = new DetailAdapter(this, this);
recyclerView.setAdapter(adapter);
// TODO: 3/1/19 change it to single ID // DetailViewModelFactory(); // id != null ? id : parentId
DetailViewModelFactory detailViewModelFactory = new DetailViewModelFactory(id != null ? id : parentId);
DetailActivityViewModel viewModel = ViewModelProviders.of(this, detailViewModelFactory).get(DetailActivityViewModel.class);
FirebaseListLiveData<Data> liveData = viewModel.getLiveDataQuery();
liveData.observe(this, adapter::setNewData);
}
#Override
public void onDialogClicked(#NonNull String text) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(HtmlCompat.fromHtml(text, 0, null, new HandlerHtml()));
builder.setPositiveButton("Ok", null);
builder.show();
}
#Override
public void onDetailClicked(Data data) {
Intent intent = new Intent();
DetailActivity.open(DetailActivity.this);
intent.putExtra("idDetail", data);
intent.putExtra("mainScreenId", id);
startActivity(intent);
}
public static void open(#NonNull Context context) {
context.startActivity(new Intent(context, InfoActivity.class));
}
}
I found a bit different, but working solution!
I create an interface
public interface SharedId {
String getSharedDataId();
String getHeader();
}
Both of my model classes Data + Title implemented Interface and methods from it.
In DetailActivity i created 2 Strings.
private String mainId;
private String detailId;
And then passed ids with my model classes with bundle
`SharedId mainId = new Title();
SharedId detailId = new Data();
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mainId = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
detailId = bundle.containsKey("idDetail") ?
bundle.getParcelable("idDetail") : null;
}
if (mainId != null) {
this.detailId = mainId.getSharedDataId();
tvToolbarTitle.setText(mainId.getHeader());
}
if (detailId != null) {
this.mainId = detailId.getSharedDataId();
tvToolbarTitle.setText(detailId.getHeader());
}
And passed in my ViewmodelFactory
DetailViewModelFactory detailViewModelFactory =
new DetailViewModelFactory(this.detailId != null ?
this.detailId : this.mainId);
I am trying to save an ArrayList of objects (List shelfItems) in the bundle to retrieve it next time the activity is opened.
(the activity gets info from firestore and I want to decrease reads and take away loading time each time the activity is opened).
but i get this error message:
savedInstanceState.putParcelableArrayList("key", shelfItems);
"putParcelableArrayList(java.lang.String, java.util.ArrayList)' in 'android.os.Bundle' cannot be applied to '(java.lang.String, java.util.List)"
This is my object class:
import android.os.Parcel;
import android.os.Parcelable;
public class ShelfItem implements Parcelable{
private String mTitle;
private String mAuthor;
private String mThumbnail;
private long mRating;
private long mEndDate;
private long mBeginDate;
private String mId;
private long mPages;
private boolean mVisible;
//make ShelfItem object
public ShelfItem(String title, String author, String thumbnail, long rating, long beginDate, long endDate, String id, long pages, boolean visible) {
mTitle = title;
mAuthor = author;
mThumbnail = thumbnail;
mRating = rating;
mBeginDate = beginDate;
mEndDate = endDate;
mId = id;
mPages = pages;
mVisible = visible;
}
public String getTitle() {
return mTitle;
}
public String getAuthor() {
return mAuthor;
}
public String getThumbnail() {
return mThumbnail;
}
public long getRating() {
return mRating;
}
public long getBeginDate() {
return mBeginDate;
}
public long getEndDate() {
return mEndDate;
}
public String getId() {
return mId;
}
public long getPages() {
return mPages;
}
public boolean getVisible() {
return mVisible;
}
public ShelfItem(Parcel in) {
mId = in.readString();
mTitle = in.readString();
mAuthor = in.readString();
mThumbnail = in.readString();
mBeginDate = in.readLong();
mEndDate = in.readLong();
mPages = in.readLong();
mVisible = in.readByte() != 0;
mRating = in.readLong();
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeString(mId);
out.writeString(mTitle);
out.writeString(mAuthor);
out.writeString(mThumbnail);
out.writeLong(mBeginDate);
out.writeLong(mEndDate);
out.writeLong(mPages);
out.writeByte((byte) (mVisible ? 1 : 0));
out.writeLong(mRating);
}
public static final Parcelable.Creator<ShelfItem> CREATOR = new Parcelable.Creator<ShelfItem>() {
public ShelfItem createFromParcel(Parcel in) {
return new ShelfItem(in);
}
public ShelfItem[] newArray(int size) {
return new ShelfItem[size];
}
};
}
and this is how I try to save the list:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelableArrayList("key", shelfItems);
}
As we learn Java, we're taught to use the interface type (List) instead of the implementation type (ArrayList) when we declare our variables. You probably have code somewhere that looks like this:
List<ShelfItem> shelfItems = new ArrayList<>();
However, in the particular case of Bundle and saving lists, you must use ArrayList specifically, and not any List in general.
If I'm right, and your list is declared like I've shown above, just change it to explicitly use ArrayList:
ArrayList<ShelfItem> shelfItems = new ArrayList<>();
If you're getting the list from somewhere else, and you can't control the implementation type of it, you can construct a new ArrayList when you need to save it:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
ArrayList<ShelfItem> toSave = new ArrayList<>(shelfItems);
savedInstanceState.putParcelableArrayList("key", toSave);
}
I need to implement parcelable in my custom class "ArtistInfo"
with the following structure:
public class ArtistInfo implements Parcelable {
private String artist;
// album name to list of ids of songs
private HashMap> albumInfo;
// song id to songInfo
private SparseArray songsMap;
protected ArtistInfo(Parcel in) {
artist = in.readString();
}
public static final Creator CREATOR = new Creator() {
#Override
public ArtistInfo createFromParcel(Parcel in) {
return new ArtistInfo(in);
}
#Override
public ArtistInfo[] newArray(int size) {
return new ArtistInfo[size];
}
};
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public void addSongsInfoToAlbum(List songsInfo, String album) {
if (albumInfo == null) {
albumInfo = new HashMap();
}
if (songsMap == null) {
songsMap = new SparseArray();
}
List songsIds = new ArrayList();
for (SongInfo songInfo : songsInfo) {
songsIds.add(songInfo.getId());
songsMap.put(songInfo.getId(), songInfo);
}
List songsIdsForAlbum = getSongIdsForAlbum(album);
songsIdsForAlbum.addAll(songsIds);
albumInfo.put(album, songsIdsForAlbum);
}
private List getSongIdsForAlbum(String album) {
if (albumInfo == null) {
return new ArrayList();
}
List songsIds = albumInfo.get(album);
return songsIds == null ? new ArrayList() : songsIds;
}
public HashMap> getAlbumInfo() {
return albumInfo;
}
public SparseArray getSongsMap() {
if (songsMap == null) {
songsMap = new SparseArray();
}
return songsMap;
}
#Override
public String toString() {
return "ArtistInfo{" +
"artist='" + artist + '\'' +
", albumInfo=" + albumInfo.toString() +
", songsMap=" + songsMap.toString() +
'}';
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(artist);
}
}
And following is the structure of the "SongInfo" class used in the above class:
public class SongInfo implements Parcelable {
private Integer id;
private String name;
private String url;
public SongInfo(Integer id, String name, String url) {
this.id = id;
this.name = name;
this.url = url;
}
protected SongInfo(Parcel in) {
if (in.readByte() == 0) {
id = null;
} else {
id = in.readInt();
}
name = in.readString();
url = in.readString();
}
public static final Creator CREATOR = new Creator() {
#Override
public SongInfo createFromParcel(Parcel in) {
return new SongInfo(in);
}
#Override
public SongInfo[] newArray(int size) {
return new SongInfo[size];
}
};
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
if (id == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeInt(id);
}
dest.writeString(name);
dest.writeString(url);
}
}
Now as you can see there is no problem in implementing the Parcelable interface in the SongInfo class, but I am not able to understand how to read and write the albumInfo and songsMap variables in the Constructor and writeToParcel method respectively. Can someone please help me understand how should I go ahead with that. Thanks!
The idea is iterate through each item in albumInfo and songsMap then add it into Parcelable.
Write to parcel.
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(artist);
// Write album info
dest.writeInt(albumInfo.size());
for (Map.Entry<String, List<Integer>> item : albumInfo.entrySet()) {
dest.writeString(item.getKey());
dest.writeList(item.getValue());
}
// Write song map
dest.writeInt(songsMap.size());
for (int i = 0; i < songsMap.size(); i++) {
int key = songsMap.keyAt(i);
dest.writeInt(key);
dest.writeParcelable(songsMap.get(key), flags);
}
}
Read from parcel
protected ArtistInfo(Parcel in) {
artist = in.readString();
// Read album info
albumInfo = new HashMap<>();
int albumInfoSize = in.readInt();
for (int i = 0; i < albumInfoSize; i++) {
String key = in.readString();
List<Integer> value = new ArrayList<>();
in.readList(value, null);
albumInfo.put(key, value);
}
// Read song map
songsMap = new SparseArray<>();
int songsMapSize = in.readInt();
for (int i = 0; i < songsMapSize; i++) {
int key = in.readInt();
SongInfo value = in.readParcelable(SongInfo.class.getClassLoader());
songsMap.put(key, value);
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Getting Error
FATAL EXCEPTION: main
Process: com.example.wuntu.tv_bucket, PID: 3895
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.example.wuntu.tv_bucket.Models.Cast.writeToParcel(Cast.java:136)
at android.os.Parcel.writeParcelable(Parcel.java:1437)
at android.os.Parcel.writeValue(Parcel.java:1343)
at android.os.Parcel.writeList(Parcel.java:759)
at android.os.Parcel.writeValue(Parcel.java:1365)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:711)
at android.content.Intent.writeToParcel(Intent.java:8790)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3112)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1541)
at android.app.Activity.startActivityForResult(Activity.java:4284)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4231)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4568)
at android.app.Activity.startActivity(Activity.java:4536)
at com.example.wuntu.tv_bucket.Adapters.CastDetailAdapter$1.onClick(CastDetailAdapter.java:124)
at android.view.View.performClick(View.java:5698)
at android.widget.TextView.performClick(TextView.java:10908)
at android.view.View$PerformClick.run(View.java:22557)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Getting error in sending arraylist of object from adapter to other activity. I wanna send my arraylist from onBindViewHolder method of Adapter to another activity but its is showing null exception error on the Cast Class in writetoParcel Method. How to send arraylist properly?
Cast Class
public class Cast implements Parcelable {
#SerializedName("cast_id")
#Expose
private Integer castId;
#SerializedName("character")
#Expose
private String character;
#SerializedName("credit_id")
#Expose
private String creditId;
#SerializedName("gender")
#Expose
private Integer gender;
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("order")
#Expose
private Integer order;
#SerializedName("profile_path")
#Expose
private String profilePath;
public Cast(){
}
protected Cast(Parcel in) {
character = in.readString();
id = in.readInt();
name = in.readString();
profilePath = in.readString();
}
public static final Creator<Cast> CREATOR = new Creator<Cast>() {
#Override
public Cast createFromParcel(Parcel in) {
return new Cast(in);
}
#Override
public Cast[] newArray(int size) {
return new Cast[size];
}
};
public Integer getCastId() {
return castId;
}
public void setCastId(Integer castId) {
this.castId = castId;
}
public String getCharacter() {
return character;
}
public void setCharacter(String character) {
this.character = character;
}
public String getCreditId() {
return creditId;
}
public void setCreditId(String creditId) {
this.creditId = creditId;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public String getProfilePath() {
return profilePath;
}
public void setProfilePath(String profilePath) {
this.profilePath = profilePath;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i)
{
parcel.writeString(name);
parcel.writeString(profilePath);
parcel.writeString(character);
parcel.writeInt(id);
}
}
CastDetailAdapter Class
public class CastDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<Cast> detailArrayList = new ArrayList<>() ;
private UrlConstants urlConstants = UrlConstants.getSingletonRef();
private Cast cast;
private final int VIEW_ITEM = 0;
private final int VIEW_PROG = 1;
private Context context;
MovieView a;
ArrayList<Cast> FullArrayList = new ArrayList<>();
public CastDetailAdapter(MovieView movieView, ArrayList<Cast> detailArrayList,ArrayList<Cast> subCastArrayList)
{
a = movieView;
this.detailArrayList = subCastArrayList;
this.FullArrayList = detailArrayList;
}
public class MyViewHolder1 extends RecyclerView.ViewHolder
{
ImageView cast_profile_picture;
TextView cast_name,cast_character_name;
public MyViewHolder1(View view)
{
super(view);
cast_profile_picture = (ImageView) view.findViewById(R.id.thumbnail);
cast_name = (TextView) view.findViewById(R.id.title);
cast_character_name = (TextView) view.findViewById(R.id.count);
}
}
public class FooterViewHolder1 extends RecyclerView.ViewHolder
{
TextView view_more;
public FooterViewHolder1(View itemView) {
super(itemView);
view_more = (TextView) itemView.findViewById(R.id.view_more);
}
}
#Override
public int getItemViewType(int position) {
if (isPositionItem(position))
return VIEW_ITEM;
return VIEW_PROG;
}
private boolean isPositionItem(int position) {
return position != getItemCount() -1;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
context = parent.getContext();
if (viewType == VIEW_ITEM)
{
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cast_details, parent, false);
return new MyViewHolder1(v);
} else if (viewType == VIEW_PROG){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.footer_layout_movie_details, parent, false);
return new FooterViewHolder1(v);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if(holder instanceof MyViewHolder1)
{
cast = detailArrayList.get(position);
((MyViewHolder1)holder).cast_character_name.setText(cast.getCharacter());
((MyViewHolder1)holder).cast_name.setText(cast.getName());
String url3 = urlConstants.URL_Image + cast.getProfilePath();
Picasso.with(context)
.load(url3)
.into(((MyViewHolder1)holder).cast_profile_picture);
}
else if (holder instanceof FooterViewHolder1)
{
((FooterViewHolder1)holder).view_more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Intent intent = new Intent(context,CastViewActivity.class);
intent.putParcelableArrayListExtra("LIST",FullArrayList);
context.startActivity(intent);
}
});
}
}
#Override
public int getItemCount() {
return this.detailArrayList.size();
}
}
In your writeToParcel() method, you have
parcel.writeInt(id);
Since id is Integer, this is going to auto-unbox id. If id is null, this auto-unboxing will throw a NullPointerException.
Since there is no Parcel.writeInteger() method, you're going to have to record whether or not id is null in a separate write. Something like:
if (id == null) {
dest.writeInt(0);
}
else {
dest.writeInt(1);
dest.writeInt(id);
}
And to read it back out:
int hasId = in.readInt();
if (hasId == 1) {
id = in.readInt();
}
else {
id = null;
}
The order in which you read the values from the parcel has to be the same as the order it was written to it.
Try:
protected Cast(Parcel in) {
name = in.readString();
profilePath = in.readString();
character = in.readString();
id = in.readInt();
}