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 trialSTEPHEN MUSONDA
2,146 PointsHaving Problem Passing Values between Activities
I am currently working on Interactive Story but the name that I type in the main activity is not appearing on the story activity vie. Below is the code in both the main activity and the story activity. Each time I run the app the String "My Friend" is appearing in the log output. I am using API level 23. I am stuck and I would appreciate any assistance.
//Code in the main activity
public class MainActivity extends Activity { 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();
startStory(name);
}
});
}
private void startStory(String name){
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra(getString(R.string.key_name),name);
startActivity(intent);
}
}
//Code in the story activity public class StoryActivity extends Activity {
public static final String TAG = StoryActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story);
Intent intent = new Intent();
String name = intent.getStringExtra(getString(R.string.key_name));
//To avoid a hard crash add the following
if(name == null){
name = "My Friend";
}
Log.d(TAG,name);
}
}
Output from Log 06-28 22:57:15.314 2624-2634/org.myowndomain.myname.interactivestory I/art: Background sticky concurrent mark sweep GC freed 13389(1927KB) AllocSpace objects, 9(208KB) LOS objects, 65% free, 1327KB/3MB, paused 5.825ms total 43.203ms 06-28 22:57:15.701 2624-2766/org.adventist.nzu.musondas.interactivestory D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 06-28 22:57:15.938 2624-2766/org.myowndomain.myname.interactivestory I/OpenGLRenderer: Initialized EGL, version 1.4 06-28 22:57:16.920 2624-2624/org.myowndomain.musondas.interactivestory I/Choreographer: Skipped 48 frames! The application may be doing too much work on its main thread. 06-28 22:57:26.879 2624-2624/org.myowndomain.myname.interactivestory D/StoryActivity: My Friend 06-28 22:57:27.193 2624-2766/org.myowndomain.musondas.interactivestory E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f49942d36c0 06-28 22:57:27.196 2624-2766/org.myowndomain.myname.interactivestory D/OpenGLRenderer: endAllStagingAnimators on 0x7f4993ba8800 (RippleDrawable) with handle 0x7f4993bb79e0
1 Answer
Kourosh Raeen
23,733 PointsHi Stephen - In StoryActivity the line:
Intent intent = new Intent();
should be:
Intent intent = getIntent();
STEPHEN MUSONDA
2,146 PointsSTEPHEN MUSONDA
2,146 PointsThank you Kourosh for you answer. It saved me several minutes of shooting in the dark.
Kourosh Raeen
23,733 PointsKourosh Raeen
23,733 PointsGlad I could help