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
Viviana Bedoya Castro
2,991 PointsInteractive Story : When the first page is displayed the text part with the name doesn't get updated.
Hello and thanks for taking the time to read this.When i start the interactive story activity and set my name, after click the start activity button, when the first page of the story is displayed my name is not updated.
In the LOgCat im gettin this:
06-19 16:01:09.609 2740-2758/com.bearandbunny.interactivestory W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-19 16:01:09.609 2740-2758/com.bearandbunny.interactivestory W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb43d12c0, error=EGL_SUCCESS
This is my code:
public class StoryActivity extends ActionBarActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Button mChoise1;
private Button mChoise2;
private String mName;
@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(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mChoise1 = (Button)findViewById(R.id.choise1Button);
mChoise2 = (Button)findViewById(R.id.choise2Button);
loadPage();
}
private void loadPage(){
Page page = mStory.getPage(0);
Drawable drawable = getResources().getDrawable(page.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = page.getText();
pageText = String.format(pageText, mName);
mTextView.setText(page.getText());
mChoise1.setText(page.getChoise1().getText());
mChoise2.setText(page.getChoise2().getText());
}
}
Quizás quisiste decir: apreciaría mucho si me pudieran ayudar I would appreciate if you could help me.
Viviana Bedoya Castro
2,991 PointsHello James, this is the MainActivity: package com.bearandbunny.interactivestory.UI;
import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;
import com.bearandbunny.interactivestory.R;
public class MainActivity extends ActionBarActivity {
private EditText mNameField;
private Button mStartButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameField = (EditText) findViewById(R.id.nameEditText);
mStartButton = (Button) findViewById(R.id.startButton);
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mNameField.getText().toString();
//Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
//Add the main activity so it wont take the onClickListener as the main class. If not, an error will occur.
startStory(name);//name esta entre parentesis para poder ser usado en starStory
}
});
}
//
public void startStory(String name){
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("name", name);
startActivity(intent);
}
}
3 Answers
Jon Kussmann
Courses Plus Student 7,254 PointsIn the loadPage() method of your StoryActivity, you should change:
mTextView.setText(page.getText());
to
mTextView.setText(pageText);
Currently you are adding the default text back to the textview, and not the formatted text with the name .
James Simshaw
28,738 PointsHello,
I would suggest either trying to change
intent.putExtra("name", name);
in your main activity to
intent.putExtra(getString(R.string.key_name), name);
or to change
mName = intent.getStringExtra(getString(R.string.key_name));
in your StoryActivity to
mName = intent.getStringExtra("name");
To make sure they're reading from the same key. This may or may not help, but it will at least make sure they're trying for the same extra.
Viviana Bedoya Castro
2,991 PointsThank you very much, ¡¡ it works now!!. I'm very grateful with both of you James and Jon.
James Simshaw
28,738 PointsJames Simshaw
28,738 PointsHello,
Could you also post where you call StoryActivity from(most likely your MainActivity)? My guess is that there was either an issue getting the name from the EditText, an issue in putting the data in the intent, or a mismatch in the key values for the extra.