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 trialTERRENCE MAVHUNGA
6,053 PointsFinally, fix the onButtonClicked() method so that the mTransitionManager successfully transitions to the new current sce
im not getting the question
public class MainActivity extends Activity {
protected TransitionManager mTransitionManager;
protected Scene mScene1;
protected Scene mScene2;
protected Scene mCurrentScene;
protected TransitionSet mForwardSet;
protected TransitionSet mBackwardSet;
// Some code omitted for brevity
public void onButtonClicked(View view) {
if (mCurrentScene == mScene2) {
mCurrentScene = mScene1;
}
else {
mCurrentScene = mScene2;
}
mTransitionManager.transitionTo(mCurrentScene);
}
protected void setupTransitions() {
// These methods are setting up scenes and transitions sets like in the video
initScenes();
initSets();
// Start here!
TransitionManager mTransitionManager = new TransitionManager();
mTransitionManager.setTransition(mScene1,mScene2, mForwardSet);
mTransitionManager.setTransition(mScene2,mScene1, mBackwardSet);
mScene1.enter();
}
}
2 Answers
Steve Hunter
57,712 PointsHi there,
This is a very easy mistake to make!!
What you have done is redeclared mTransitionManager
inside the setupTransitions
method. This makes that instance local in scope to that method only. So, outside of that method, the mTransitionManager
is not initialized resulting in a Null Pointer Exception.
To fix this remove the preceding TransitionManager
as the code already knows that mTransitionManager
is of type TransitionManager
. Don't redeclare it in the setupTransitions()
method, just initialize it; i.e. just assign something to mTransitonManager
, as you have done but without the formal data type.
See the comments in here:
public class MainActivity extends Activity {
protected TransitionManager mTransitionManager; // declared as 'TransitionManager' here
.
.
.
public void onButtonClicked(View view) {
if (mCurrentScene == mScene2) {
mCurrentScene = mScene1;
}
else {
mCurrentScene = mScene2;
}
mTransitionManager.transitionTo(mCurrentScene);
}
protected void setupTransitions() {
initScenes();
initSets();
// Not this, don't use the data type again; it already is of the right type
// TransitionManager mTransitionManager = new TransitionManager();
/* but this - no preceding data type as this
is already declared at the top of the class */
mTransitionManager = new TransitionManager();
.
.
Let me know if that's not clear!
Steve.
TERRENCE MAVHUNGA
6,053 PointsSteve Hunter, hey thts where i was missing it : mTransitionManager = new TransitionManager(); thanx hey
Steve Hunter
57,712 PointsNo problem!
Melody Mudehwe
3,282 PointsSorry guys I am still getting make sure you transition to correct the scene.
Steve Hunter
57,712 PointsCan you post your code, Melody?