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
mersit ukosata
736 Pointsfun facts crashes on avd
after i set the on click listener for the button and write the next fun fact which is "did you know ostriches run faster then horses" i run the app and on the avd it says "unfortunately fun facts has stopped working
2 Answers
Ben Jakuben
Treehouse TeacherWhen the app crashes, it should have some information in the logcat view in Android Studio. (If you are missing the logcat view, click on the tab "6: Android" at the bottom and then make sure the "Devices | logcat" tab is selected.)
Let us know what the errors are in there and we can help you troubleshoot.
mersit ukosata
736 PointsOk so i just did that and now i got an error
package com.example.mers.funfacts2;
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;
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 view from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button nextButton;
Button = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
// The button was clicked, so update the fact label with new fact
String fact = "Ostriches can run faster then horses";
factLabel.setText(fact);
}
};
Button.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);
}
private class Button {
}
}
Ben Jakuben
Treehouse TeacherSorry, I missed this reply in here. The ID I was referring to was the R.id.button part. You need to change this:
Button nextButton;
Button = (Button) findViewById(R.id.button);
to this:
Button nextButton = (Button) findViewById(R.id.nextButton);
nextButton will be the variable you use in the rest of your code. So you'll need to change this line, for example:
Button.setOnClickListener(listener);
to this instead:
nextButton.setOnClickListener(listener);
mersit ukosata
736 Pointsmersit ukosata
736 PointsHey Ben thanks for the help. So this is what im getting on my logcat:
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherThanks! Okay, so this is a lot of information, but the lines we are most interested are these:
That tells us the problem and where it occurred. Check out line 31 in FunFactsActivity.java. If you can't figure out what the error is, paste all your code in here from FunFactsActivity.java and we'll help you troubleshoot. (If you paste it in, let us know which line is line 31.)
mersit ukosata
736 Pointsmersit ukosata
736 PointsOk so i tried fixing it by deleting line 31 and the app opens up but the button doesnt click to go to the next fun fact and then when i put that line back in the app crashes. I have watched the video again to see if my code looks like yours and it does so im not sure what im doing wrong.... So this is my code and i put "line#31" to let you know which line is 31. Note: I named my button "next" button
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherThings look okay there. Can you paste in the contents of activity_fun_facts.xml? Perhaps an ID or something is wrong in there.
mersit ukosata
736 Pointsmersit ukosata
736 PointsOnce again thank you so much for the help Ben i really appreciate it
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherOkay, it looks like your IDs are mismatched. In your Java you are trying to set the Button variable using
nextButtonas the ID:Button nextButton = (Button) findViewById(R.id.nextButton);But in your XML, the Button has the ID of just
button:To fix it, change
R.id.nextButtontoR.id.buttonin the Java line and you should be set. :)mersit ukosata
736 Pointsmersit ukosata
736 Pointsthis is the part where i get the error
on (Button) findViewById(R.id.button) has a red line under it and also under that where it says Button.setOnClickListener (listener) the word setOnClickListener is red