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 trialFabian Claudio
4,017 PointsFun Fact has stopped
Hey guys the app wont run...I checked the other solutions like changing styles.xml to another code but it just showed up red. Here's the code, styles.xml and manifest.xml
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 the views from our layout file
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 to show a new fact
String fact = "Ostriches can run faster than horses";
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);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="claudio.fabian.funfacts" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FunFactsActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
2 Answers
adsdev
15,583 PointsFabian Claudio . You did the onclicklistener outside the on create method. Ill show you the difference
//This is what you posted
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
}
// Declare our view variables and assign the views from our layout file
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 to show a new fact
String fact = "Ostriches can run faster than horses";
factLabel.setText(fact);
showFactButton.setOnClickListener(listener);
}
};
//Heres what it should look like
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Declare our view variables and assign the views from our layout file
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 to show a new fact
String fact = "Ostriches can run faster than horses";
factLabel.setText(fact);
showFactButton.setOnClickListener(listener);
}
};
}
Hope it helps. If you have more questions feel free to ask :)
adsdev
15,583 PointsFabian Claudio . You did the onclicklistener outside the on create method. Ill show you the difference
//This is what you posted
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
}
// Declare our view variables and assign the views from our layout file
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 to show a new fact
String fact = "Ostriches can run faster than horses";
factLabel.setText(fact);
showFactButton.setOnClickListener(listener);
}
};
//Heres what it should look like
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Declare our view variables and assign the views from our layout file
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 to show a new fact
String fact = "Ostriches can run faster than horses";
factLabel.setText(fact);
showFactButton.setOnClickListener(listener);
}
};
}
Hope it helps. If you have more questions feel free to ask :)
Fabian Claudio
4,017 PointsFabian Claudio
4,017 PointsWow thanks I was super angry lol solved it