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 The Rest of the Story Loading Additional Pages

Mohammed Ashah Uddin
Mohammed Ashah Uddin
1,417 Points

Getting an error when clicking the choice button.

Whenever I click a choice button, the app stops working. The logcat says that the "drawable" is causing the problem.

public class StoryActivity extends AppCompatActivity {

    private String name;
    private Story story;
    private ImageView storyImageView;
    private TextView storyTextView;
    private Button choice1Button;
    private Button choice2Button;

    @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(getString(R.string.key_name));
        if(name.isEmpty() || name == null)
        {
            name = "Ahmed";
        }

        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();
                LoadPage(nextPage);
            }
        });
        choice2Button.setText(page.getChoice2().getTextId());
        choice2Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int nextPage = page.getChoice2().getNextPage();
                LoadPage(nextPage);
            }
        });
    }
} 

2 Answers

Mohammed Ashah Uddin
Mohammed Ashah Uddin
1,417 Points

I figured it out, took some time but it worked finally. My page constructor was not setup properly. Simple.

collin banhagel
collin banhagel
3,677 Points

I'm having a similar issue what was it exactly my page constructor looks fine to me.

The pasting code the in the teacher code was incorrect. In the story class, when initializing Page objects, the code switches the parameters position between textId and ImageId. You just need to swap their positions.