I'm trying to send two custom objects, one that stores an instance of another, along with an intent to another activity. Both of these implement Serializable, like so:
public class Story implements Serializable {
private String title, author;
private ArrayList<StorySegment> segments;
... }
class StorySegment implements Serializable {
private String author;
private String contents;
... }
And in my StoryOverviewAdapter class, I've got this:
public class StoryOverviewAdapter extends ArrayAdapter<Story> {
public StoryOverviewAdapter(Context ctx, ArrayList<Story> stories) {
super(ctx, 0, stories);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.story_overview_item, parent, false);
}
final Story story = getItem(position);
TextView tvStoryTitle = (TextView) convertView.findViewById(R.id.tv_story_title);
tvStoryTitle.setText(story.getTitle());
TextView tvStoryOpening = (TextView) convertView.findViewById(R.id.tv_story_opening);
tvStoryOpening.setText(story.getSegment(0).getContents());
ImageView ivUserPicture = (ImageView) convertView.findViewById(R.id.iv_user_picture);
ivUserPicture.setTag(position);
ivUserPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show user profile
}
});
LinearLayout llStoryLayout = (LinearLayout) convertView.findViewById(R.id.story_layout);
llStoryLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), StoryActivity.class);
intent.putExtra("story", story);
getContext().startActivity(intent);
}
});
return convertView;
}
}
When I go to pull this information from my Intent in the receiving activity...
Intent intent = getIntent();
Story story = (Story) intent.getSerializableExtra("story");
... And then try to test my code, I get this:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
And I have no idea why. I thought maybe removing 'final' from the story variable would get rid of it, but I can't access the variable from inside my inner OnClickListener class without it. Not sure what to do.
EDIT: Also, here's the code that shows that I've added a story...
public class OverviewActivity extends AppCompatActivity {
ArrayList<Story> stories = new ArrayList<Story>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
stories.add(new Story("Lorem Ipsum", "Firstname Lastname",
new StorySegment("Firstname Lastname", "Lorem ipsum dolor sit amet, lorem ipsum dolor sit amet, lorem ipsum dolor sit amet...")));
StoryOverviewAdapter storyOverviewAdapter = new StoryOverviewAdapter(this, stories);
ListView listView = (ListView) findViewById(R.id.story_overview_list);
listView.setAdapter(storyOverviewAdapter);
}
}