Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Fragments Managing Fragments Adding the ViewPager

SmellsLikeBacon crash on orientation change, cannot cast ViewPagerFragment to ListFragment

So everything works as instructed but when I change orientation on my phone (SMT211) app crashes, stacktrace:

04-21 12:57:52.905 21194-21194/com.thetechguysblog.smellslikebacon E/AndroidRuntime: FATAL EXCEPTION: main Process: com.thetechguysblog.smellslikebacon, PID: 21194 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thetechguysblog.smellslikebacon/com.thetechguysblog.smellslikebacon.MainActivity}: java.lang.ClassCastException: com.thetechguysblog.smellslikebacon.ViewPagerFragment cannot be cast to com.thetechguysblog.smellslikebacon.ListFragment

Just putting it out there.

Go to the video named "Initializing a Fragment with data" in this same section at 3:45 in the video and he explains how to fix it.

Essentially in the MainActivity when you first create the fragment and use the findFragmentById(R.id.placeholder) it will retrieve the ViewPagerFragment if thats the one you are on. To fix this you need to set a tag for each fragment and use findFragmentByTag([TAG_NAME]) instead.