Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:04:16+00:00 2026-05-26T02:04:16+00:00

I am trying to create array of buttons, then making one onClick event for

  • 0

I am trying to create array of buttons, then making one onClick event for all, here is my code, when I run the code I get (The application testing(processcom.MyTest.testing) has stopped unexpectedly. Please try again) I am not sure what, where is the error. I must be missing something fundamental, I am not trying to create app, I am just leanring Java, specially for Android.

Here is my code

package com.MyTest.testing;


import static android.widget.Toast.makeText;
import java.util.Random;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;


public class TreActivity extends Activity {
      /** Called when the activity is first created. */
//declaring some public int vars

int isPressed1 = 1;
int isPressed2 = 1;
int isPressed3 = 1;
int isPressed4 = 1;
int isPressed5 = 1;
int isPressed6 = 1;
int isPressed7 = 1;
int isPressed8 = 1;
int isPressed9 = 1;
int isPressed10 = 1;





@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


   myMethod();





}; //End of onCreate (Bundle savedInstanceState) 

public void myMethod() { 
    MyClickHandler handler = new MyClickHandler();  

    Button[] buttons = fnButtonArray();
    for (Button button : buttons) {  
       button.setOnClickListener(handler);  
       }  
 }  


class MyClickHandler implements View.OnClickListener {  
      public void onClick(View v) { 
        Toast.makeText(TreActivity.this, ((Button) v).getText() , Toast.LENGTH_SHORT).show();
      }  
 }


public Button[] fnButtonArray(){

    Button arrButtons[] = new Button[11];
      // started with 1 not 0 for the array to match the button numbers
    arrButtons[1] = (Button) findViewById(R.id.btn1);
    arrButtons[2] = (Button) findViewById(R.id.btn2);
    arrButtons[3] = (Button) findViewById(R.id.btn3);
    arrButtons[4] = (Button) findViewById(R.id.btn4);
    arrButtons[5] = (Button) findViewById(R.id.btn5);
    arrButtons[6] = (Button) findViewById(R.id.btn6);
    arrButtons[7] = (Button) findViewById(R.id.btn7);
    arrButtons[8] = (Button) findViewById(R.id.btn8);
    arrButtons[9] = (Button) findViewById(R.id.btn9);
    arrButtons[10]= (Button) findViewById(R.id.btn10);
    return arrButtons;

  }
  } //public class TreActivity extends Activity
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:04:17+00:00Added an answer on May 26, 2026 at 2:04 am

    It looks like you’re getting a NullReferenceException. You create an array of size 26 but only fill the 2nd through 11th elements. Then you loop through all 26 elements trying to assign a click handler. When you try to assign a click handler to a null element, an exception is thrown.

    You’ll probably be able to see these details by checking LogCat. You can access LogCat in Eclipse or run adb logcat from the terminal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an array in MIPS Assembly, and then add all the
Im trying to create an array of tm* structs, and then return them at
I am trying to create an array of functions so I can call one
I am trying to create an array of class objects taking an integer argument.
I'm trying to create an array to display the last 5 products a customer
I am trying to create an array or list that could handle in theory,
I'm trying to create an array of strings that can be randomized and limited
I'm trying to create an array (States) of arrays (Cities). Whenever I try to
I'm trying to create an array of structs and also a pointer to that
I have a small assignment in C. I am trying to create an array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.