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
Eric Dotson
1,933 PointsI having trouble with my code I add the views and my code went crazy the editor says I have 8 errors
private CrystalBall2 mCrystalBall = new CrystalBall2();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our View variables and assign them the Views from the layout file
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String answer = mCrystalBall.getAnAnswer();
// Update the label with our dynamic answer
answerLabel.setText(answer);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3 Answers
cbcbcb
16,560 Points1) answerLabel.setText( answer.getText( ).toString( ) );
2) Delete the bottom onCreateOptionsMenu( ) method starting at @Override just above it and everything else below
3) You apparently declared a View object of type TextView. Delete that.
4) activity_main is not the name of your xml file under ../res/layouts/
5) Go to that folder. Three scenarios...See below
5a) You did name your layout that.
5b) You don't even have a layout.
5c) You named your xml layout file something other than "main_activity"
6) if scenario = 5a then do next step | | if scenario = 5b skip next step and do step 7 | | if scenario = 5c skip next two steps and do step 8
7) Press ctrl+s, then in toolbar up top left click project->clean->clean all projects, then go back to your .java file and press ctrl+shift+O, ctrl+s, //DONE
8) Make your layout file and try again. Make sure you name it main_activity.xml //DONE
9) go back to your .java file and replace setContentView(R.layout.main_activity); with setContentView(R.layout.<name of your xml file ); //CONTINUE
10 ) If you just finished step 9 go back two steps to number 7. //CONTINUE
Gunjeet Hattar
14,483 PointsCould you tell us what those errors are?
From what I can infer at this point you can check if
you imported the library files for Button and TextView
made a typo error in some other place in the program
cbcbcb
16,560 Pointsoops I didn't mean to comment you.
Eric Dotson
1,933 Pointsactivity_main cannot be resolved or is not a field Multiple markers at this line - Line breakpoint:MainActivity [line: 21] - onCreate(Bundle) - textView1 cannot be resolved or is not a field
activity_main cannot be resolved or is not a field
Duplicate method onCreateOptionsMenu(Menu) in type MainActivity
main cannot be resolved or is not a field
cbcbcb
16,560 Pointsping..see my answer