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 trialAlice Spencer
12,273 PointsName field not being returned in storry
This has me stumped. I have added
<string name="key_name">name</string>
to my strings.xml file
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mNameField.getText().toString();
startStory(name);
}
});
}
private void startStory(String name) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra(getString(R.string.key_name),name);
startActivity(intent);
}
to my MainActivity.java file
and
mName = intent.getStringExtra(getString(R.string.key_name));
to my StoryActivity.java file
BUT...when I run the program and enter my name, the next page, which is the first page of the story still shows up with the %1$s instead of my name.
I've gone over the videos, and cannot figure out where I've deviated from the correct code.
3 Answers
Mike Sheldon
14,897 PointsThis is the code that I have for the pageText
around line 53 of StoryActivity.java
String pageText = mCurrentPage.getText();
// Add the name if placeholder included. Won't add if no placeholder
pageText = String.format(pageText, mName);
mTextView.setText(pageText);
Alice Spencer
12,273 PointsYes, I did set the intent. I even took it out and redid it just to make sure I didn't have an error, no luck, but thanks.
Harley Urquhart
26,203 Pointstry cleaning the project if you have done everything right then it might just be AS playing around with you
Mike Sheldon
14,897 PointsI was having the same issue and noticed that "pageText" was grayed out in my code with gray squiggly lines under it and a warning that said:
"The value String.format(pageText, mName) assigned to 'pageText' is never used"
This was in the line: ``` pageText = String.format(pageText, mName);
Looking through my code I found that the next line below that was incorrect.
Here is what I had: ```
mTextView.setText(page.getText());
And here is what it needed to be: ``` mTextView.setText(pageText);
This replacement happened about 25 seconds into the video and I had missed it and this is what was breaking it for me.
Alice Spencer
12,273 PointsThat doesn't seem to be it either. This is what I've got in StoryActivity
//Add the name if placeholder included. Won't add if no placeholder
String pageText = mCurrentPage.getText();
mTextView.setText(mCurrentPage.getText());
I tried replacing those to lines, and got errors.
Ruggiero A
8,534 PointsAlice Spencer it can seem a silly mistake but.. did you set your mNameField correctly?
mNameField = (EditText) findViewById(R.id.editText);
Ruggiero A
8,534 PointsRuggiero A
8,534 PointsDid you actually set the intent in the StoryActivity.java file?
Intent intent = getIntent();