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
Glen Hayes
5,798 PointsPrivate app coding help
Hi guys,
I'm attempting a personal gym app to help me keep track of my exercises and reps etc.
I am already running into some basic problems that I don't fully understand?
Firstly, I have set up a whole heap of activities for the different windows I have for the app is the best way to do it?).I was going through and setting up button intents for onclicklisteners and everything was going ok (MainActivity to UserDetails to Phases) but when I put a new onclicklistener in the Phases java class, my UserDetails onclicklistener button stopped working and shut the app down?
If someone could take a look at the code and tell me where this novice is going wrong it would be greatly appreciated. I have no errors in my code according to Android Studio.
MainActivity code:
public class MainActivity extends ActionBarActivity { private Button mStartButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mStartButton = (Button) findViewById(R.id.startButton);
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startUserDetails = new Intent(MainActivity.this, UserDetails.class);
tapToStart();
}
});
}
private void tapToStart() {
Intent startUserDetails = new Intent(this, UserDetails.class);
startActivity(startUserDetails);
}
}
UserDetails code:
public class UserDetails extends MainActivity { private Button mContinueUserDetailsButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_details);
Button mContinueUserDetailsButton = (Button) findViewById(R.id.continueUserDetailsButton);
mContinueUserDetailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
continueUserDetails();
}
});
}
private void continueUserDetails () { Intent startBodyPartSelect = new Intent(this, Phases.class); startActivity(startBodyPartSelect); }
}
Phases code:
public class Phases extends UserDetails { private Button mPhase1; private Button mPhase2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phases);
Button mPhase1 = (Button) findViewById(R.id.buttonPanel);
Button mPhase2 = (Button) findViewById(R.id.buttonPhase2);
mPhase1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
mPhase2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void phaseSelect() {
if(mPhase1.isPressed()) {
Intent startPhase1 = new Intent(Phases.this, Phase1.class);
}
else if(mPhase2.isPressed()) {
Intent startPhase2 = new Intent(Phases.this, Phase2.class);
}
}
}
5 Answers
Jon Kussmann
Courses Plus Student 7,254 PointsHi Glen,
Your UserDetails Activity should not extend MainActivity, it should be extending ActionBarActivity. The same with your Phases Activity.
I hope this helps, if not let me know.
Glen Hayes
5,798 PointsHi there Jon,
Thanks for the assist! Unfortunateley, this does not work for me? It does until I try to put more intent code in for the next set of buttons to select phases from the Phase class. I have no errors but when touching the button in the UserDetails activity in the app, it still gives me the error "Unfortunately, app has stopped".
I'm confused as to why it wont work as the button on the UserDetails activity will take me to the Phases Activity as long as I have no intent coding on the Phases class?
Thanks for your help.
Jon Kussmann
Courses Plus Student 7,254 PointsIt looks like you were trying to declare a method within another method. Try this:
public class Phases extends ActionBarActivity {
private Button mPhase1; private Button mPhase2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phases);
Button mPhase1 = (Button) findViewById(R.id.buttonPanel);
Button mPhase2 = (Button) findViewById(R.id.buttonPhase2);
mPhase1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startPhase1 = new Intent(Phases.this, Phase1.class);
startActivity(startPhase1)
}
});
mPhase2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startPhase2 = new Intent(Phases.this, Phase2.class);
startActivity(startPhase2);
}
});
}
}
Kristian Terziev
28,449 PointsPlease see this post regarding your code formatting since it's impossible to read that way. Thanks
Glen Hayes
5,798 PointsThanks again Jon!
I'm sure what you suggested would have ultimately helped in the long run with my app as a whole, but after tweaking the code with your input it was still throwing me an error.
Stupidly, what was happening though was I had misspelled an id for the mPhase1 button! As I am still very much a beginner, I dont quiet understand fully why mis spelling an id that has nothing to do with the intent I am currently working with can cause the app to fail? Android studio wasnt even throwing any errors with the wrongly spelled id?
Thanks again for your help, I really appreciate it.
Jon Kussmann
Courses Plus Student 7,254 PointsNear the beginning of the onCreate method of your Activity you have:
Button mPhase1 = (Button) findViewById(R.id.buttonPanel);
The R.id.buttonPanel part needs to have the exact same spelling as in the xml file. This is how your Activity code knows which button is which. Android Studio wasn't throwing any initial errors because buttonPanel exists, just not in the layout file that corresponds to the Activity you are working in.
It does have to do with the Intent because the Intent is made inside the onClick method of the button. If the button is not made properly (if it doesn't correspond to a button in the actual layout) then that Intent will never be called. That button cannot be clicked on because it doesn't exist in the current layout.
Unfortunately Android Studio only checks if the ID exists somewhere in the project. It doesn't check to see if the ID exists in the layout file that you are currently working with.
Glen Hayes
5,798 PointsAhh ok. Thank you!
Glen Hayes
5,798 PointsAhh ok. Thank you!
Glen Hayes
5,798 PointsGlen Hayes
5,798 PointsDo I just do all the intents and coding for the different activities on the MainActivity page?