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

Android

Android Multiple Buttons

While building a simple Android app that has two button in MainActivity. Both button should open a separate activity. However, only one button is working while the other makes the app crash. What am I doing wrong?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnContact_1 = (Button) findViewById(R.id.btnContact_1);
    btnContact_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, JohnRodContactInfo.class));
        }
    });

    Button btnContact_2 = (Button) findViewById(R.id.btnContact_2);
    btnContact_2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, JaneRodContactInfo.class));
        }
    });



}

}

btnContact_2 is not working... please help

2 Answers

Well the issue was not the code. I had an image that was too large on that particular activity and it made the app close when the activity was called.

hmm... well the if the first button worked. I can't see anything wrong with the second button. Have you taken a look at your XML file, perhaps your pulling the wrong ID or not setting it correctly. Check if the new activity your trying to start is created correctly.