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 Build an Interactive Story App (Retired) Intents and Multiple Activities Introducing String Resources

Alice Spencer
Alice Spencer
12,273 Points

Name 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.

Ruggiero A
Ruggiero A
8,534 Points

Did you actually set the intent in the StoryActivity.java file? Intent intent = getIntent();

3 Answers

This 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
Alice Spencer
12,273 Points

Yes, 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.

try cleaning the project if you have done everything right then it might just be AS playing around with you

I 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
Alice Spencer
12,273 Points

That 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
Ruggiero A
8,534 Points

Alice Spencer it can seem a silly mistake but.. did you set your mNameField correctly? mNameField = (EditText) findViewById(R.id.editText);