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
Caleb Kleveter
Treehouse Moderator 37,862 PointsInteractive story won't change pages.
I am finishing up the interactive story app, but I have run into an issue; I can't go to the next page after I start the story. Basically, I enter my name and go to the first page, but when I try to click on a button nothing happens.
StoryActivity.java
package calebkleveter.interactivestory.ui;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
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 calebkleveter.interactivestory.R;
import calebkleveter.interactivestory.model.Page;
import calebkleveter.interactivestory.model.Story;
import static calebkleveter.interactivestory.R.id.storyImageView;
public class StoryActivity extends Activity {
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story);
Intent intent = getIntent();
mName = intent.getStringExtra(getString(R.string.key_name));
if (mName == null) {
mName = "Friend";
}
Log.d(TAG, mName);
mImageView = (ImageView) findViewById(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);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
pageText = String.format(pageText, mName);
mTextView.setText(pageText);
if (mCurrentPage.isFinal()) {
mChoice1.setVisibility(View.INVISIBLE);
mChoice2.setText("PLAY AGAIN");
mChoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
} else {
mChoice1.setText(mCurrentPage.getChoice1().getText());
mChoice2.setText(mCurrentPage.getChoice2().getText());
mChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int nextPage = mCurrentPage.getChoice1().getNextPage();
loadPage(nextPage);
}
});
mChoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int nextPage = mCurrentPage.getChoice2().getNextPage();
loadPage(nextPage);
}
});
}
}
}
Ben Deitch
Treehouse TeacherBen Deitch
Treehouse TeacherJust took a quick glance; nothing's jumping out at me. If you put a Breakpoint (Or Log message or Toast) inside the onClick methods you should at least be able to tell if they're getting called or not. Maybe getNextPage is returning the wrong page?