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 Build an Interactive Story App (Retired) Finishing the User Interface Formatting Strings

The drawable() is deprecated and the drawable() with theme parameter is not running the app.

The drawable() is deprecated and the drawable() with theme parameter is not running the app. It is showing an error that the app suddenly stopped. The page 0 is fine but it is not continuing on to the next Page.

Error Sequence:

FATAL EXCEPTION: main Process: com.example.ayush.interactivestory, PID: 4323 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ayush.interactivestory/com.example.ayush.interactivestory.ui.StoryActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to android.widget.TextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to android.widget.TextView at com.example.ayush.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:42) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Kevin Perez
Kevin Perez
Courses Plus Student 8,180 Points

It's beacause you are trying to cast an "AppCompatImageView" to an "android.widget.TextView".

check your log (Line Nº 3):

"ClassCastException:

android.support.v7.widget.AppCompatImageView cannot be cast to android.widget.TextView"

For example:

private ImageView mImageView;

mImageView = (ImageView) findViewById(R.id.storyImageView);

And this is an Alternative for: Drawable drawable = getResources().getDrawable(page.getImageId());

Drawable drawable = ResourcesCompat.getDrawable(getResources(), mCurrentPage.getImageId(), null);

1 Answer

Thanks