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 Wrapping Up

samuel zaffran
samuel zaffran
24,815 Points

App Crash

Hi, Can someone explain me why my code crash at final pages ? Thank's. Everything is ok except for those 2 last pages, so i'm posting only the code relevant, i guess the mistake come from here or there.

public class Suite extends AppCompatActivity {

private String mName;
private Story mStory = new Story();

private ImageView mImageView;
private TextView mTextView;
private Button mChoice1;
private Button mChoice2;
private Page mCurrentPage;

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

    mImageView = (ImageView)findViewById(R.id.imageView2);
    mTextView = (TextView)findViewById(R.id.textView);
    mChoice1 = (Button)findViewById(R.id.button2);
    mChoice2 = (Button)findViewById(R.id.button3);

    Intent intent = getIntent();
    mName = intent.getStringExtra(getString(R.string.name));
    if(mName == null) mName = "Friend";

    loadPage(0);
}

private void loadPage(int nextPage){
    mCurrentPage = mStory.getPage(nextPage);

    Drawable drawable = ResourcesCompat.getDrawable(getResources(), mCurrentPage.getImageView(), null);
    mImageView.setImageDrawable(drawable);

    String text = mCurrentPage.getTextView();
    text = String.format(text, mName);
    mTextView.setText(text);

    mChoice1.setText(mCurrentPage.getChoice1().getText());
    mChoice2.setText(mCurrentPage.getChoice2().getText());

    if(mCurrentPage.isFinal()){
        mChoice2.setVisibility(View.INVISIBLE);
        mChoice1.setText("PLAY AGAIN");
        mChoice1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    } else {
        mChoice1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int next = mCurrentPage.getChoice1().getNextPage();
                loadPage(next);
            }
        });

        mChoice2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int next = mCurrentPage.getChoice2().getNextPage();
                loadPage(next);
            }
        });
    }
}

}

public class Page {

private int mImageView;
private String mTextView;
private Choice mChoice1;
private Choice mChoice2;
private boolean mIsFinal = false;


public boolean isFinal() {
    return mIsFinal;
}

public void setFinal(boolean isFinal){mIsFinal = isFinal;}

public Page(int id, String text) {
    mImageView = id;
    mTextView = text;
    mChoice1 = null;
    mChoice2 = null;
    mIsFinal = true;
}

public Page (int id, String text, Choice choice1, Choice choice2){
    mImageView = id;
    mTextView = text;
    mChoice1 = choice1;
    mChoice2 = choice2;
}

public int getImageView(){
    return mImageView;
}

public void setImageView(int id){
    mImageView = id;
}

public String getTextView() {
    return mTextView;
}

public void setTextView(String textView) {
    mTextView = textView;
}

public Choice getChoice1() {
    return mChoice1;
}

public void setChoice1(Choice choice1) {
    mChoice1 = choice1;
}

public Choice getChoice2() {
    return mChoice2;
}

public void setChoice2(Choice choice2) {
    mChoice2 = choice2;
}

}

Could you post the LogCat? You can narrow it down by selecting "Error" in the Log level dropdown box.

samuel zaffran
samuel zaffran
24,815 Points

Thank you, but i solved it, it was only about putting those two lines in the else section :

mChoice1.setText(mCurrentPage.getChoice1().getText()); mChoice2.setText(mCurrentPage.getChoice2().getText());

If it can help someone in the future :)