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

Eleni Minadaki
Eleni Minadaki
3,687 Points

Button doesn't work.

Hi everyone!I create an app based on interactive story app lessons. The app has 2 Buttons, previous and next and 31 articles. The buttons works normal till the last article. When previous Button has tapped at the last article,app has stopped. Thanks for any help! StoryActivity.java import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.TextureView; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.ScrollView; import android.widget.TextView;

import com.example.eleni.interactivestory.R; import com.example.eleni.interactivestory.model.Page; import com.example.eleni.interactivestory.model.Story;

import static android.widget.TextView.*;

public class StoryActivity extends AppCompatActivity {

public static final String TAG = StoryActivity.class.getSimpleName();


private Story mStory = new Story();

private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;


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


  //Intent intent = getIntent();
   //mName = intent.getStringExtra(getString(R.string.key_name));

    if(mName == null){
        mName = "Friend";
    }

    Log.d(TAG, mName);

    mImageView = (ImageView)findViewById(R.id.storyImageView);
    mTextView = (TextView)findViewById(R.id.storyTextView);
   mPreviousButton = (Button)findViewById(R.id.previousButton);
   mNextButton = (Button)findViewById(R.id.nextButton);

    mTextView.setMovementMethod(new ScrollingMovementMethod());


    loadPage(0);
}
private void loadPage(int choice){
 mCurrentPage = mStory.getPage(choice);

    Drawable drawable  = getResources().getDrawable(mCurrentPage.getImageId());
    mImageView.setImageDrawable(drawable);

    String pageText = mCurrentPage.getText();
    //add the name if placeholder included.
   //pageText  = String.format(pageText,mName);
    mTextView.setText(pageText);

    if(mCurrentPage.isSingle()){
        mPreviousButton.setVisibility(View.VISIBLE);
        mNextButton.setText("Start From The Begining");
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadPage(1);
            }
        });

    }else{

        mPreviousButton.setText(mCurrentPage.getChoices().getText1());
        mNextButton.setText(mCurrentPage.getChoices().getText2());

        mPreviousButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int previousPage = mCurrentPage.getChoices().getPreviousPage();
                loadPage(previousPage);
            }
        });

        mNextButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            int nextPage = mCurrentPage.getChoices().getNextPage();
            loadPage(nextPage);
        }
    });
}}

} Choices.java public class Choices { private String mText1; private String mText2;

private int mPreviousPage;
private int mNextPage;



public Choices(int currentPage, String text1, String text2) {
    mPreviousPage = currentPage - 1;
    mText1 = text1;
    mNextPage = currentPage + 1;
    mText2 = text2;

}

public String getText1() {

    return mText1;
}

public void setText1(String text1) {

    mText1 = text1;
}

public String getText2(){
    return mText2;
}

public void setText2(String text2){
  mText2 = text2;
}

public int getPreviousPage() {
  return mPreviousPage;
}

public void setPreviousPage(int previousPage){
  mPreviousPage = previousPage;
}

public int getNextPage() {

    return mNextPage;
}

public void setNextPage(int nextPage) {
    mNextPage = nextPage;
}

} Page.java public class Page {

private int mPageId;
private int mImageId;
private String mText;
private Choices mChoices;
private boolean mIsSingle = false;

public Page(int pageId, int imageId, String text, Choices choices) {

    mPageId = pageId;
    mImageId = imageId;
    mText = text;
    mChoices = choices;
}

public Page(int pageId, int imageId, String text) {

    mPageId = pageId;
    mImageId = imageId;
    mText = text;
    mChoices = null;
    mIsSingle = true;
}
public int getPageId(){
    return mPageId;
}

public void setPageId(int pageId){
    mPageId = pageId;
}

public int getImageId() {
    return mImageId;
}

public void setImageId(int imageId) {
    mImageId = imageId;
}

public String getText() {
    return mText;
}

public void setText(String text){
    mText = text;
}

public Choices getChoices(){
    return mChoices;
}

public void setChoices(Choices choices){
    mChoices = choices;
}

    public boolean isSingle() {

        return mIsSingle;
    }

    public void setSingle(boolean isFinal) {

        mIsSingle = isFinal;
    }
  }

2 Answers

Sang Tran
Sang Tran
6,918 Points

Hi Eleni, there was a similar problem we had during the interactive story course. Try implementing the finish() when the button is clicked. Maybe this might help. Here's the link https://teamtreehouse.com/library/build-an-interactive-story-app/finishing-the-user-interface/ending-the-story

Eleni Minadaki
Eleni Minadaki
3,687 Points

Hi Sang, Thanks a lot for your answer. The nextButton works fine with this way and start from the beginning. But I can't make previous button work. The problem is only in my last article of the app. From article 31 can't goes back to 30! If you could tell me something more specific,I 'll appreciate. Thanks a lot!