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 trialLuke Horvath
590 PointsContextCompat.getDrawable(this, get.ImageId()); isn't returning the image and crashes.
private void loadPage(int pageNumber) {
Page page = story.getPage(pageNumber);
Drawable pic = ContextCompat.getDrawable(this, page.getImageId());
storyImageView.setImageDrawable(pic);
}
I have back traced my code and it all lines up. I remade the code 4+ times and have spent hours trying to find an alternative to still return the page determined by the path of the user input. He did say that there are many different methods but I gather, through exhausting alternatives, this is the main on used for backwards compatibility through APIs.
Any help would be greatly appreciated. Using IceSandwich4.2 API15 if that helps.
Luke Horvath
590 PointsHey Ben. Thanks for getting back to me. Here is the code from the logcat.
01-01 00:03:59.480 1640-1640/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.phantom.interactivestory/com.example.phantom.interactivestory.UI.StoryActivity}: android.content.res.Resources$NotFoundException: File On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: "Help me, %1$s, you're my only hope." from drawable resource ID #0x7f060026
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: "Help me, %1$s, you're my only hope." from drawable resource ID #0x7f060026
at android.content.res.Resources.loadDrawable(Resources.java:1975)
at android.content.res.Resources.getDrawable(Resources.java:660)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
at com.example.phantom.interactivestory.UI.StoryActivity.loadPage(StoryActivity.java:51)
at com.example.phantom.interactivestory.UI.StoryActivity.onCreate(StoryActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.FileNotFoundException: On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: "Help me, %1$s, you're my only hope."
at android.content.res.AssetManager.openNonAssetNative(Native Method)
at android.content.res.AssetManager.openNonAsset(AssetManager.java:407)
at android.content.res.Resources.loadDrawable(Resources.java:1967)
at android.content.res.Resources.getDrawable(Resources.java:660)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
at com.example.phantom.interactivestory.UI.StoryActivity.loadPage(StoryActivity.java:51)
at com.example.phantom.interactivestory.UI.StoryActivity.onCreate(StoryActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Ben Jakuben
Treehouse TeacherThis line seems to indicate a problem with how the resources are being set somewhere in your Story or Page classes. Can you take a look and/or paste in that code?
Caused by: android.content.res.Resources$NotFoundException: File On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: "Help me, %1$s, you're my only hope." from drawable resource ID #0x7f060026
It almost seems like page.getImageId() is returning a String ID instead of a Drawable ID. I wonder if you maybe mixed up the order of the parameters in the Story class? Here's what I have:
pages[0] = new Page(R.drawable.page0,
R.string.page0,
new Choice(R.string.page0_choice1, 1),
new Choice(R.string.page0_choice2, 2));
3 Answers
Luke Horvath
590 PointsHey Ben,
It's taken me so long to get back to you but I can not find the solution anywhere!
I have navigated through the project tab and made sure that everything is where it's supposed to be. I followed the string value back to the strings.xml and made sure that the image is a drawable by changing the code to load the image as a tool for our view and also to just load the static image on our first page oncreate.
It's really confusing me as my code is exactly the same as yours in the video and i've triple checked it twice now haha.
Is there any alternative code that can be used to set an image in relation to variable such as the code we are using in the video here?
Im on my laptop right now, rather than my desktop, so I won't be able to post code but I can assure you, it's the same. Could it be something that has changed recently?
Ben Jakuben
Treehouse TeacherI'm sorry you're still struggling with this! At this point it might be best for you to zip up your code and email it to me (ben[at]teamtreehouse[dot]com). While you're waiting for me to look, you could also continue with the course by just setting one image in the layout itself and not trying to set it in code. You could take the rest of the course and then come back to fix this last issue.
Luke Horvath
590 PointsHey Ben.
I have tried writing a couple of emails, one with the attachment and one without, and its returning with a failure to send because your email provider is blocking the email :(
This is never-ending haha.
Do you have an alternative address to send stuff to?
Ben Jakuben
Treehouse TeacherOh no - that's new! Can you upload the zip file somewhere, like Google Drive or Dropbox, and send me a link? Or if you use GitHub, can you create a repo and send me a link to that? This is an interesting issue, so I'm curious to see what the root of it is.
Ben Jakuben
Treehouse TeacherBy the way, I tried to recreate this with my own API 15 emulator, but this code is working fine for me. My troubleshooting sense is telling me there's a problem in your code somewhere, though it's certainly possible it could be in the setup. Hope you can share an upload!
Luke Horvath
590 PointsAny luck? It's a tricky one :D
Ben Jakuben
Treehouse TeacherIt took me a while to trace this one through, but my original assumption was correct. Here's how you are creating pages in the Story class:
pages[0] = new Page(R.drawable.page0,
R.string.page0,
new Choice(R.string.page0_choice1, 1),
new Choice(R.string.page0_choice2, 2));
Note that the first parameter (line 1) is the image, and the second (line 2) is the page text.
Now, check out your Page constructor:
public Page(int textId, int imageId, Choice choice1, Choice choice2) {
this.textId = textId;
this.imageId = imageId;
this.choice1 = choice1;
this.choice2 = choice2;
}
The first parameter is for the text, and the second is for the image. They're swapped! So your app is trying to load the text into the image, and the image into a TextView, and is crashing with the original error. You just need to switch the order of the parameters in the parentheses there and it works! :)
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherCan you copy and paste your stack trace in here when it crashes? (This will be all the red text that shows up in the logcat view in Android Studio.)