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 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.