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
Farouk Charkas
1,957 PointsCan anyone fix this crash?
I have created an app that display different facts with their photos, but it crashes. It does not contain any errors or warnings. This is my code:
package everingeducational.oceanical;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView factPhoto;
final TextView theFact;
Button anotherFact;
factPhoto = (ImageView) findViewById(R.id.factPhoto);
theFact = (TextView) findViewById(R.id.theFact);
anotherFact = (Button) findViewById(R.id.anotherFact);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] factBook = {
"Scientists say that octopuses are technically aliens.",
"Of the more than 500 or so shark species, about 80% grow to less than 1.6 meters.",
"Almost any shark 1.8 meters or longer is a potential danger, but three species have been identified repeatedly in fatal bites: great whites, tigers, and bull sharks.",
"An estimated 50-80% of all life on earth is found under the ocean surface."
};
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(factBook.length);
if (randomNumber == 0) {
factPhoto.setImageResource(R.drawable.octopus_alien_discovery);
}
else if (randomNumber == 1) {
factPhoto.setImageResource(R.drawable.shark_species_laura);
}
else if (randomNumber == 2) {
factPhoto.setImageResource(R.drawable.shark_long_evolution);
}
else {
factPhoto.setImageResource(R.drawable.coral_life_constant);
}
String fact = factBook[factBook.length];
theFact.setText(fact);
}
};
anotherFact.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.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
1 Answer
Jon Kussmann
Courses Plus Student 7,254 PointsHi Evering,
Your error comes from this line:
String fact = factBook[factBook.length];
You will get an ArrayIndexOutOfBounds error because the length of your factBook is 4, but you can only get up to index 3 (since the size of factBook is 4, you have indices 0-3).
I believe you meant to write:
String fact = factBook[randomNumber];
I hope this helps. If not let me know.
Farouk Charkas
1,957 PointsFarouk Charkas
1,957 PointsThank you, Jon Kussmann Next time I will review my code before I ask