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 trialJohn Weland
42,478 PointsAndroid Activity Lifecycle: code challenge 3 of 3
I am back again, maybe it is too late in the day or I don't have enough coffee. I cannot for the life of me get this to pass.
"In the onRestoreInstanceState method retrieve the value stored in the bundle. Then set the text of our TextView to be equal to the saved value."
This is the closest I've gotten which throws an error "Bummer! There is no resource associated with id: 10".
import android.content.Intent;
import android.view.View;
public class MainActivity extends Activity {
public TextView mTextView;
public static final String KEY_USERENTRY = "KEY_USERENTRY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_USERENTRY, 10);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mTextView.setText(savedInstanceState.getInt(KEY_USERENTRY));
}
}
2 Answers
John Weland
42,478 Points@Joey Mejias
mTextView.setText(Integer.toString(savedInstanceState.getInt(KEY_USERENTRY)));
can also be evaluated as
mTextView.setText(savedInstanceState.getInt(KEY_USERENTRY) + "");
savedInstanceState.getInt(KEY_USERENTRY) returns an int
it's been a while since I was stuck here but let us assume that it evaluates to the number 42 by adding + "" to it so 42 + "" it evaluates to the string interpretation of that number or "42"
Moderator Edited: Change response from Comment to Answer so it may be marked as Best Answer"
Ben Deitch
Treehouse TeacherDarn. I was hoping that error message would be enough to trigger an 'aha' moment.
There's more than one definition for TextView.setText(): setText(int) is for when we want to set the text from a String resource and so it expects a resource ID. setText(String) is for when we want to set it to a String.
Edit: I added a bit to the error message. Hopefully it should be bit less confusing for all future students!
John Weland
42,478 PointsI changed it to
mTextView.setText(Integer.toString(savedInstanceState.getInt(KEY_USERENTRY)));
It worked but it feels so verbose, I am not sure THAT is what you were looking for.
Ben Deitch
Treehouse TeacherYep, that works, but adding, + "" , to automatically upcast it to a String is more concise. I forget where Ben J mentions it, but I know he talks about 'mooshing' them together.
John Weland
42,478 PointsI can't remember where but I do remember it was somewhere... StormyApp maybe?
Joey Mejias
Courses Plus Student 9,137 PointsThis is still confusing.
Can the code below and the + "" be clarified more, please? Thanks.
mTextView.setText(Integer.toString(savedInstanceState.getInt(KEY_USERENTRY)));
Kyle Baker
8,211 PointsKyle Baker
8,211 PointsJoey Mejias "savedInstanceState.getInt(KEY_USERENTRY) retrieves an integer and we need a string when we "setText". So, we can convert that integer into a string with "mTextView.setText(Integer.toString(savedInstanceState.getInt(KEY_USERENTRY)))" or we can do "mTextView.setText(savedInstanceState.getInt(KEY_USERENTRY)+"")".