Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build an Interactive Story App (Retired) The Model-View-Controller Pattern Adding Custom Constructors

Eleni Minadaki
Eleni Minadaki
3,687 Points

When Button has Tapped App stops.

Hi everyone! I am a newbie trying to make an app based on interactiveStory App. The app works ok, but when the second Button (back button) has tapped app stops. Something goes wrong with my code But I can't find!! The point is each time backButton has tapped,going 1 page back, as the 1st button do with the nextPage. Thanks for any help!

MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.eleni.interactivestory.R;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private TextView meditCenterText;
    private Button mStartButton;

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

        meditCenterText = (TextView) findViewById(R.id.editCenterText);
        mStartButton = (Button) findViewById(R.id.startButton);

        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String TextView = meditCenterText.getText().toString();
                startStory(TextView);
            }
        });
    }

    private void startStory(String TextView) {
        Intent intent = new Intent(this, StoryActivity.class);
        intent.putExtra("TextView", TextView);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

        meditCenterText.setText("");
    }
}
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.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 Button mChoice1;
    private Button mChoice2;
    private String mName;
    private Page mCurrentPage;
    private Page mPreviousPage;


    @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);
        mChoice1 = (Button)findViewById(R.id.choiceButton1);
        mChoice2 = (Button)findViewById(R.id.choiceButton2);

        loadPage(0);
    }
    private void loadPage(int choice){
     mCurrentPage = mStory.getPage(choice);
        //edw prepei na kanw kati gia to previous
        //mPreviousPage = 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.isFinal()){
            mChoice1.setVisibility(View.INVISIBLE);
            mChoice2.setText("Start From The Begining");
            mChoice2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   finish();
                }
            });

        }else{

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

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

        mChoice2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPreviousPage.getChoice2().getPreviousPage();
                int previousPage = mPreviousPage.getChoice2().getPreviousPage();
                loadPage(previousPage);
            }
        });


    }
}}
Choice.java
public class Choice {
    private String mText;
    private int mNextPage;
    private int mPreviousPage;


    public Choice(String text, int nextPage) {
        mText = text;
        mNextPage = nextPage;

    }

    public String getText() {

        return mText;
    }

    public void setText(String text) {

        mText = text;
    }

    public int getNextPage() {

        return mNextPage;
    }

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

 public int getPreviousPage() {

     return mPreviousPage;
   }

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

   }
}
Page.java
public class Page {
    private int mImageId;
    private String mText;
    private Choice mChoice1;
    private Choice mChoice2;
    private boolean mIsFinal = false;

    public Page(int imageId, String text, Choice choice1, Choice choice2){
        mImageId = imageId;
        mText = text;
        mChoice1 = choice1;
        mChoice2 = choice2;


    }
    public Page(int imageId, String text){
        mImageId = imageId;
        mText = text;
        mChoice1 = null;
        mChoice2 = null;
        mIsFinal = true;
    }



    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 Choice getChoice1() {
        return mChoice1;
    }

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

    public Choice getChoice2() {
        return mChoice2;
    }

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

        public boolean isFinal() {

            return mIsFinal;
        }

        public void setIsFinal(boolean isFinal) {

            mIsFinal = isFinal;
        }
      }

[MOD: edited for code block]