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
Charles Brown
2,135 PointsSyntax Errors
Hello everyone, I am new here and hoping someone can help me with a problem I am having. I am currently trying to build my first application (Crystal Ball) however I continue to run into some coding issues. I have followed the instructions in the video verbatim I continue to get syntax errors on the last 4 lines of the Mainactivity file I have deleted and restarted four projects just to start from scratch below is the problems I am encountering
package com.example.crystalball;
import java.util.Random;
import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected 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() {
@Override
public void onClick(View V) {
String[]answers = {
"It is certain",
"All signs say Yes",
"It is decidedly so",
"The stars are not aligned",
"My reply is no",
"It is doubtful",
"Better not tell you now",
"Concentrate and ask again",
"unable to answer now" };
}
// This button was clicked, so update the answer label with an answer.
String answer = "";
//Randomly select one of three answers
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(answers.length());
answer = answers[randomNumber];
answerLabel.setText(answer);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2 Answers
bobkingstone
27,869 PointsHi
You have one too many } after the answers array :
"unable to answer now" };
} //here - remove this one
// This button was clicked, so update the answer label with an answer.
String answer = "";
Also if you have problems like this you can download the project files to compare your code against that in the videos.
Regards Bob
Charles Brown
2,135 PointsThank you so much for your help your suggestion worked perfectly. Now only if there was a faster Emulator :-)
Regards Charlie Brown