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 trialRoland Joseph
1,210 PointsI can't get my button to work. I typed in the java code just like the video, but nothing happens when I click my button
All I see is the original text about ants stretching. It doesn't switch to the text about Ostriches after clicking the button
Roland Joseph
1,210 PointsHere is my code...
package com.rojo.funfacts;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class FunFactsActivity extends AppCompatActivity { // Declare our view variables private TextView mFactTextView; private Button mShowFactButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Assign the views from the layout file in the corresponding variables
mFactTextView = (TextView) findViewById(R.id.factTextView);
mShowFactButton = (Button) findViewById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// The button was clicked, so update the fact TextView with a new fact
String fact = "Ostriches can run faster than horses.";
mFactTextView.setText(fact);
}
};
mShowFactButton.setOnClickListener(listener);
}
}
4 Answers
Roland Joseph
1,210 PointsI just had to relaunch the emulator and it worked.
Roland Joseph
1,210 PointsThank your Grigorij!!
I relaunched the emulator and it worked.
Grigorij Schleifer
10,365 PointsGreat !
Always the easy things that makes us crazy :)
And I am sure that your code you have posted in the beginning would also work fine.
Grigorij Schleifer
10,365 PointsHi Roland,
here is my code suggestion:
package com.example.nexuskiller.testproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fact = "Ostriches can run faster than horses.";
mTextView.setText(fact);
}
});
}
}
I have implemented the onClick() and setText() logic inside the setOnClickListener().
Let me know if it was helpful.
Grigorij
Roland Joseph
1,210 PointsThank you for you help. I used your suggestion but it still isn't working. Here is my code...
package com.rojo.funfacts;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class FunFactsActivity extends AppCompatActivity { // Declare our view variables private TextView mFactTextView; private Button mShowFactButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Assign the views from the layout file in the corresponding variables
mFactTextView = (TextView) findViewById(R.id.factTextView);
mShowFactButton = (Button) findViewById(R.id.showFactButton);
mShowFactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// The button was clicked, so update the fact TextView with a new fact
String fact = "Ostriches can run faster than horses.";
mFactTextView.setText(fact);
}
});
}
}
Grigorij Schleifer
10,365 PointsTo be honest, I have no clue why the code isnΒ΄t working. Have you checked the xml file? IdΒ΄s are set properly? Reloaded the project? Tried to run the app on the physical phone?
Let me know if you have found a trick
Grigorij
omprakash krishna
7,068 PointsRoland Joseph....your code work fine!!!..thnx a lot...
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHey Rolang,
can you paste the code plz?