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 Finishing the User Interface Formatting Strings

Caleb Seeling
Caleb Seeling
1,189 Points

My App crashes

When I try to open Interactive Story, the App crashes.This is the Error:

10-23 12:20:57.891 4581-4581/? E/Zygote: v2 10-23 12:20:57.901 4581-4581/? E/Zygote: accessInfo : 0 10-23 12:21:07.591 4581-4581/com.teamtreehouse.interactivestory E/AndroidRuntime: FATAL EXCEPTION: main Process: com.teamtreehouse.interactivestory, PID: 4581 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teamtreehouse.interactivestory/com.teamtreehouse.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 #0x7f060022 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3320) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) at android.app.ActivityThread.access$1100(ActivityThread.java:229) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7407) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 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 #0x7f060022 at android.content.res.Resources.loadDrawableForCookie(Resources.java:4287) at android.content.res.Resources.loadDrawable(Resources.java:4156) at android.content.res.Resources.getDrawable(Resources.java:2045) at android.content.res.Resources.getDrawable(Resources.java:2027) at android.content.Context.getDrawable(Context.java:464) at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30) at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372) at com.teamtreehouse.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:61) at com.teamtreehouse.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:55) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3267) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)  at android.app.ActivityThread.access$1100(ActivityThread.java:229)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:7407)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  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:456) at android.content.res.Resources.loadDrawableForCookie(Resources.java:4280) at android.content.res.Resources.loadDrawable(Resources.java:4156)  at android.content.res.Resources.getDrawable(Resources.java:2045)  at android.content.res.Resources.getDrawable(Resources.java:2027)  at android.content.Context.getDrawable(Context.java:464)  at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)  at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)  at com.teamtreehouse.interactivestory.ui.StoryActivity.loadPage(StoryActivity.java:61)  at com.teamtreehouse.interactivestory.ui.StoryActivity.onCreate(StoryActivity.java:55)  at android.app.Activity.performCreate(Activity.java:6904)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3267)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)  at android.app.ActivityThread.access$1100(ActivityThread.java:229)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:7407)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Caleb Seeling
Caleb Seeling
1,189 Points

My Code

StoryActivity:

package com.teamtreehouse.interactivestory.ui;

import android.content.Intent; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

import com.teamtreehouse.interactivestory.R; import com.teamtreehouse.interactivestory.model.Page; import com.teamtreehouse.interactivestory.model.Story;

import static android.R.attr.name;

public class StoryActivity extends AppCompatActivity {

private static final String TAG = StoryActivity.class.getSimpleName();
private Story story;
private ImageView storyImageView;
private TextView storyTextView;
private Button choice1Button;
private Button choice2Button;
private String name;






@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    storyImageView = (ImageView) findViewById(R.id.storyImageView);
    storyTextView = (TextView)findViewById(R.id.storyTextView);
    choice1Button = (Button)findViewById(R.id.choice1Button);
    choice2Button = (Button)findViewById(R.id.choice2Button);

    Intent intent = getIntent();
    name = intent.getStringExtra("name");
    if (name == null || name.isEmpty()) {
        name = "Friend";

    }
    Log.d(TAG,name);


    story = new Story();
    loadPage(0);
}

private void loadPage(int pageNumber) {
     final Page page = story.getPage(pageNumber);

    Drawable Image = ContextCompat.getDrawable(this, page.getImageId());
    storyImageView.setImageDrawable(Image);

    String pageText= getString(page.getTextId());
    pageText = String.format(pageText, name);
    storyTextView.setText(pageText);

    choice1Button.setText(page.getChoice1().getTextId());
    choice1Button.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int nextPage = page.getChoice1().getNextPage();
        }
    });
    choice2Button.setText(page.getChoice2().getTextId());

    choice2Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int nextPage = page.getChoice2().getNextPage();
        }
    });


}

}

Page.java

package com.teamtreehouse.interactivestory.model;

/**

  • Created by Caleb on 21.10.17. */

public class Page { private int textId; private int imageId; private Choice choice1; private Choice choice2; private boolean isFinalPage = false;

public Page(int textId, int imageId) {
    this.textId = textId;
    this.imageId = imageId;
    this.isFinalPage = true;
}

public Page(int textId, int imageId, Choice choice1, Choice choice2) {
    this.textId = textId;
    this.imageId = imageId;
    this.choice1 = choice1;
    this.choice2 = choice2;
}

public int getTextId() {
    return textId;
}

public void setTextId(int textId) {
    this.textId = textId;
}

public boolean isFinalPage() {
    return isFinalPage;
}

public void setFinalPage(boolean finalPage) {
    isFinalPage = finalPage;
}

public int getImageId() {
    return imageId;
}

public void setImageId(int imageId) {
    this.imageId = imageId;
}

public Choice getChoice1() {
    return choice1;
}

public void setChoice1(Choice choice1) {
    this.choice1 = choice1;
}

public Choice getChoice2() {
    return choice2;
}

public void setChoice2(Choice choice2) {
    this.choice2 = choice2;
}

}

Choice.java

package com.teamtreehouse.interactivestory.model;

/**

  • Created by Caleb on 21.10.17. */

public class Choice {

private int TextId;
private int nextPage;


public Choice(int textId, int nextPage) {
    this.TextId = textId;
    this.nextPage = nextPage;
}

public int getTextId() {
    return TextId;
}

public void setTextId(int textId) {
    TextId = textId;
}

public int getNextPage() {
    return nextPage;
}

public void setNextPage(int nextPage) {
    this.nextPage = nextPage;
}

}

Story.java

package com.teamtreehouse.interactivestory.model;

import com.teamtreehouse.interactivestory.R;

import static com.teamtreehouse.interactivestory.R.string.page6;

/**

  • Created by Caleb on 21.10.17. */

public class Story {

private Page[] pages;






public Story() {

    pages = new Page[7];

    pages[0] = new Page(R.drawable.page0,
            R.string.page0,
            new Choice(R.string.page0_choice1, 1),
            new Choice(R.string.page0_choice2, 2 ));

    pages[1] = new Page(R.drawable.page1,
            R.string.page1,
            new Choice(R.string.page1_choice1, 3),
            new Choice(R.string.page1_choice2, 4));

    pages[2] = new Page(R.drawable.page2,
            R.string.page2,
            new Choice(R.string.page2_choice1, 4),
            new Choice(R.string.page2_choice2, 6));

    pages[3] = new Page(R.drawable.page3,
            R.string.page3,
            new Choice(R.string.page3_choice1, 4),
            new Choice(R.string.page3_choice2, 5));

    pages[4] = new Page(R.drawable.page4,
            R.string.page4,
            new Choice(R.string.page4_choice1, 5),
            new Choice(R.string.page4_choice2, 6));

    pages[5] = new Page(R.drawable.page5, R.string.page5);

    pages[6] = new Page(R.drawable.page6, R.string.page6);

}


public Page getPage(int pageNumber) {
    return pages[pageNumber];
}

}

1 Answer

You are not setting the nextPage (you are not calling the setNextPage() method), so when you try to call .setText(), your app crashes.

Caleb Seeling
Caleb Seeling
1,189 Points

I don't know what I shall change. Could you give me the code?I changed what I understood you said and corrected the code above...Still, doesn't work.