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 trialTimothy Williams
1,536 PointsLesson correct? I am stuck. showFactButton.setOnClickListener(listener); causes crash
Let me say this: I like the IDEA of Treehouse. I like the 3 JAVA lessons on this Android Track. But they were NOT enough before jumping into this course which suddenly changes from changing color in XML to this last lesson. The code does not match the CURRENT Android Studio. I taught myself to use the debugger, step through and find the crash point (that should be ADDED to this track earlier). If I comment out //showFactButton.setOnClickListener(listener); then it runs as before the lesson, else it crashes the app. Here is the code copy/paste.
package com.shootmyphoto.funfacts;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.TextView;
public class FunFactsActivity extends ActionBarActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
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 v) {
//button clicked update fact label
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.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_fun_facts, container, false);
return rootView;
}
}
}
1 Answer
Nicolas Hampton
44,638 PointsWhen you're following along with this course, be very careful to use autofill when the instructor uses autofill, because he's not telling us about certain dependencies that android studio automatically adds when autofill is used. I had similar problems until I caught that.
Timothy Williams
1,536 PointsTimothy Williams
1,536 PointsLet me answer my own question. I do not know what a FRAGMENT is... but my XML layout had the relative layout in a fragment and there was auto-generated code referring to it. I manually moved the XML to the activity_fun_facts.xml, did a Safe Delete on the fragment_activity_fun_facts.xml after cleaning up the FunFactsActivity.java references to the fragments. I do not know how this project got into that territory, but it caused me 3-4 hours of research and some trial/error. Hope this helps someone!