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 trialBoyd Christiansen
366 PointsCompiling errors in JS that weren't happening before.
Hey! First off, thank you for taking the time to help answer my question!
Now to the problem. So, I was working on the Android programming course (I was at Android Development -> Code a Simple Android App -> Coding the Fun Facts -> Using Conditionals (If Statements)) and I decided to try a few things on my own. This came in the form of attempting to write a few of my own if statements in JS. After I had finished writing what I thought should work, I tried to compile. Nine errors appeared in parts of the code that I had not been editing, which makes me think that I accidentally changed something somewhere else that messed up the syntax for that part of the code. Looking through, I can't see exactly what that is and was hoping that you could help me with that!
I apologize for not putting this in Git, as I have no experience with it yet. Please let me know if I need to put up the entire project or move this piece into Git.
package boydharrisonchristiansen.funfacts;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class FunFactsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
//Declare our view variables and assign them the views from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
//The button was clicked, so update the fact label with a new fact
String fact = "";
//Randomly select a fact
Random randomGenerator = new Random(); //construct a new random number generator
int randomNumber = randomGenerator.nextInt(3);
fact = randomNumber + ""; //This line tests rand # generator output
/* Convert a random number to a text fact
0=Ants stretch when they wake up in the morning
1=Ostriches can run faster than horses
2=Olympic gold metals are actually made mostly of silver
*/
/*if(randomNumber == 0) {
fact = "Ants stretch when they wake up";
}
//Update the label with our dynamic fact*/
factLabel.setText(fact);
}
showFactButton.setOnClickListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fun_facts, 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);
}
}
Once again, thank you for your time!
Edit: I forgot to mention that this code is post fix attempt, where I removed all If statements, hoping that would fix it.
Also, I got a screen cap of where the errors are appearing for me: http://snag.gy/lNp7k.jpg
1 Answer
Jacob Bergdahl
29,119 PointsI think you're just missing a semicolon at the end of the listener. So, add a semicolon on your line 46, and see if it works!
Boyd Christiansen
366 PointsBoyd Christiansen
366 PointsJust took your advice and tried that. Unfortunately, the errors are still there.
Boyd Christiansen
366 PointsBoyd Christiansen
366 PointsFound that I had an earlier version of the code, right before I tried my own stuff. Everything looks like it is working like it doe before! Thanks for helping me though!
Here's the version that works if you want to look at it! ^_^