Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Movie Night in Android

Karim Zibari
Karim Zibari
7,358 Points

Movies' array not passed to MoviesActivity

Hi, I have been stuck on this all day! Probably something silly. Anyway, for project 5, I have an array holding movies objects, and I try to pass this in the MainActivity using: Intent intent=new Intent(MainActivity.this, MoviesActivity.class); intent.putExtra(MOVIES, mMovies); startActivity(intent);

Then in the MoviesActivity, I try to retrieve the array from the intent object using:

    Parcelable[] arcelables=intent.getParcelableArrayExtra(MainActivity.MOVIES);
    mMovies= Arrays.copyOf(parcelables,parcelables.length, Movie[].class);
    MovieAdapter adapter=new MovieAdapter(this, mMovies);
    mRecyclerView.setAdapter(adapter);

But the Parcelables arrays and hence the mMovies arrays always are empty. I have checked using breakpoints that the array of movies is in fact not empty and has correct data while in the MainActivity, but when control reaches the MoviesActivity, the array is empty and null. I have made the Movie class Parcelable with all the correct methods etc.
I don't know what it could be. Any help would be much appreciated.

Karim Zibari
Karim Zibari
7,358 Points

I have found the bug ( my own obviously). I changed the code so that I moved the Intent lines to inside the Button OnClick method and this seems to have resolved the issue. It must have been an issue of scope.