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

Formatting Strings %1$s Interactive Story

I don't get any errors but the string returns null instead of the name I input.

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;



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

        Intent intent = getIntent();
        String mName = intent.getStringExtra(getString(R.string.app_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();

    }

    private void loadPage(){
        Page page = mStory.getPage(0);
        Drawable drawable = getResources().getDrawable(page.getImageId());



        mImageView.setImageDrawable(drawable);
        String pageText = page.getText();

        //add name if placeholder included
        pageText = String.format(pageText, mName);
        mTextView.setText(pageText);
        mChoice1.setText(page.getChoice1().getText());
        mChoice2.setText(page.getChoice2().getText());
    }
}

Hi Chris

Have you tried running this through a debugger and putting a breakpoint on the if statement you could then see what the value of mName is when it tries the if condition. Perhaps the value is "null" or "" instead of a null value?

I fully expect if you run this through a debugger you will see the value of mName is not null and therefore your mName = "friend" line is never being executed.

Thanks Daniel

When I ran the debugger it returns the correct name. Just have to figure out where to populate the value. Can you give me a little more lead?

Hi Chris

So a quick google found this http://stackoverflow.com/questions/19590472/does-edittext-gettext-ever-returns-null

According to these guys on stack overflow an editText can never return null it instead returns an empty string. Try changing the if statement to:

if(mName.equals("")){

    mName = "Friend";

}

Hopefully this will work, if not let me know on here and I will type the code in android studio here and try it locally

Daniel

4 Answers

Thanks for taking the time to look into this. this is what ended up fixing it. just leave off the String. But why?? Is it because its already declared and putting String in front of it resets the data in the variable?

 mName = intent.getStringExtra(getString(R.string.key_name));

this and the other changes you suggested

also here is a copy of my strings.xml

<resources>
    <string name="app_name">Interactive Story</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_story">StoryActivity</string>
    <string name="key_name"></string>
</resources>

Hi Chris

I didn't even spot that fault, but yes it would be an error. The reason is because the variable is already declared inside the class as a String and you can not declare an existing variable twice within the same scope (scope means the area in which the variable can be accessed). So what you did by removing the String is changed the statement to from a declaration to an assignment (this is giving the variable a value).

Secondly I think you will still get some inconsistent results as you haven't declared a string value for the key_name string value use something like this

<resources>
    <string name="app_name">Interactive Story</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_story">StoryActivity</string>
    <string name="key_name">keyName</string>
</resources>

Good job getting rid of the error, I always find it a little more satisfying when I figure it our for myself.

Hopefully you're all good to go now

Daniel

Hi Daniel thanks. It still displays null. When I step through the code it shows the name in the console correctly.

I've been trying for days, can't get it to work. I am using Android Studio version 1.4 by the way.

Ok I will load the project files onto my computer and have a bash at it today.

Hi Chris

So I've loaded the files onto my computer and it does work as expected. if you use the correct key from your Intent it will never return null so you should use

if(mName.equals(""))
    mName = "Friend";

I did notice after looking over your code again you use the line

String mName = intent.getStringExtra(getString(R.string.app_name));

The project files use a different name for the key it is key_name so you may need to change the line of code above to:

String mName = intent.getStringExtra(getString(R.string.key_name)); 

This key name needs to match exactly the key you used when you first set up the intent in the MainActivity you should have done something similar to the below:

private void startStory(String name) {
        Intent intent = new Intent(this, StoryActivity.class);
        intent.putExtra(getString(R.string.key_name), name);
        startActivity(intent);
    }

The line

 intent.putExtra(getString(R.string.key_name), name.trim());

is where you defined the key by using getString(R.string.key_name), (I also added .trim() just to remove any whitespace before and after the name string).

Hopefully this will sort it for you, let me know if it doesn't and we'll have to rethink!

Daniel