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
Ilja Daderko
8,953 PointsIssues when adding activity listener (Android Studio & Genymotion)
Hello,
For some reason I get an error message saying "Unfortunately [My App Name Here] Has stopped"
This happens once I add an event listener for onClick, And what confuses me is that I looked through it and it looks correct + there is literary nothing in the project besides few buttons.
My main activity looks like this:
package com.example.braintraininggame.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
/*
Declare variables.
*/
Button getAboutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
/*
Assign views to variables.
*/
getAboutButton = (Button) findViewById(R.id.buttonAbout);
/*
Create Listeners.
*/
getAboutButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
/*
Methods
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 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_main, container, false);
return rootView;
}
}
}
Any ideas on what can be causing this error?
P.S I did not add my code from fragment_main.xml file so it doesn't get messy, but if you think that it could be cause by it please tell me and I'll post it here. Also, when I created my 4 buttons, the code for those went to fragment_main.xml file, but not activity_main.xml file can this be causing an issue?
1 Answer
Ben Jakuben
Treehouse TeacherIf your button is in the fragment layout, then that would be your issue. You need to initialize it and set the onClickListener in the Fragment's onCreateView() method, which we start talking about around the 2:15 mark in this video: http://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/using-fragments-for-tabs/modifying-fragments-from-the-template