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

Why is String mName is not added to the pageText (line 59 in StoryActivity.java)

For the Android Interactive Story, my code successfully gets the user's name when the user types their name in, but does not successfully insert their name into the pageText. If you want me to post any other files from the project, I can do so.

//MainActivity.java

package com.joshbgold.interactivestory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.joshbgold.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 mName = mNameField.getText().toString();
                startStory(mName);
                //Toast.makeText(MainActivity.this, mName, Toast.LENGTH_LONG).show();
            }
        });
    }

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

    /*
    @Override
    protected void onResume(){
        super.onResume();
        mNameField.setText("");
    }
    */

}

//StoryActivity.java

package com.joshbgold.interactivestory.ui;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.joshbgold.interactivestory.R;
import com.joshbgold.interactivestory.model.Page;
import com.joshbgold.interactivestory.model.Story;


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 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();
    String name = intent.getStringExtra(getString(R.string.key_name));

    if (mName == null){
        mName = "friend";
    }
    Log.d(TAG,name);

    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(0);

    }
    private void loadPage(int choice){
        mCurrentPage = mStory.getPage(choice);

        Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
        mImageView.setImageDrawable(drawable);

        String pageText = mCurrentPage.getText();

Here is the part I am having trouble with:

        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 v){
                    finish();
                }
            });
        }
        else {

            mChoice1.setText(mCurrentPage.getChoice1().getText());
            mChoice1.setText(mCurrentPage.getChoice2().getText());

            mChoice1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int nextPage = mCurrentPage.getChoice1().getNextPage();
                    loadPage(nextPage);
                }
            });

            mChoice2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int nextPage = mCurrentPage.getChoice2().getNextPage();
                    loadPage(nextPage);
                }
            });
        }
    }
}

What are you seeing in the app? What is the text that is showing up?

4 Answers

Hello,

In your onCreate() function of the StoryActivity activity, you are calling

Intent intent = getIntent();
    String name = intent.getStringExtra(getString(R.string.key_name));

    if (mName == null){
        mName = "friend";
    }

In this case, mName is never defined before the if statement. However, if you were instead to do:

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

you should get some results. The other way you could change your code is instead of changing the getStringExtra line to change the if statement to:

if (name == null) {
    mName = "friend";
}
else {
    mName = name;
}

Hi Patrick, I am just seeing "friend" in the text. However, if I do a toast message to see what is in the name variable, I do see the name that user entered. I am trying out the 2nd of the two possible solutions offered by James right now.

Hello James, I rechecked my Toast message and it is still correctly showing "Bob" or whatever name I enter when running the program.

I tried both solutions you suggested and the actual textview, however, says "null" for the name variable. I'll keep looking into it.

James, I took a little break and came back to this, and it looks like your suggestion works after all. Thanks!

I used almost exactly the code you suggested to fix the issue:

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