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

Why won't my ClickListener work?

Button[][] arr = {
            {b1,b2,b3,b4,b5},
            {b6,b7,b8,b9,b10},
            {b11,b12,b13,b14,b15},
            {b16,b17,b18,b19,b20},
            {b21,b22,b23,b24,b25},
            {b26,b27,b28,b29,b30},
            {b31,b32,b33,b34,b35},
            {b36,b37,b38,b39,b40}
};
View.OnClickListener btap = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setBackgroundColor(Color.RED);
            }
};

arr[0][0].setOnClickListener(btap);

Won't work, Please help!

(I have correctly defined and all 40 b's)

b7 Should turn red when tapped.

Can you try adding Log.i(...) inside onClick() to see if this method gets called?

When I run the app It just crashes.

1 Answer

Early on in the file there is this bit of code: setContentView(R.layout.file1);

But all 40 b's are in file2, so I tried this:

final View.OnClickListener listener2 = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.file2);
            final View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View view){view.setBackgroundColor(Color.RED);}
            };
            for(int i=0;i<=5;i++){arr[0][i].setOnClickListener(listener)}
        }
 };

There are 2 .xml file I'm Switching from