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

  • Home
  • SEARCH
  • 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 6727145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:00:48+00:00 2026-05-26T10:00:48+00:00

for (int i = 0; i < 9; i++) { b[i].setOnClickListener( new OnClickListener() {

  • 0
for (int i = 0; i < 9; i++)
{
    b[i].setOnClickListener(
        new OnClickListener()
        {
            public void onClick(View v)
            {
                justclicked(i);
            }
        }

    );
}

I am attempting to put an action listener on nine buttons using a for loop. The code above is giving me an error. Is the error caused by the value of i not being visible? Thanks a lot, world-class experts @ Stack Overflow!!

  • 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-26T10:00:48+00:00Added an answer on May 26, 2026 at 10:00 am

    No, it’s because i isn’t final, which it must be to access from an anonymous inner class.
    Add

    final int finalI = i;
    

    before

    b[i].setOnClickListener( ...
    

    and then use finalI instead of i: justclicked(finalI);.

    After that, think up a better name for i and finalI.

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

Sidebar

Related Questions

public int testb; btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { ********* }} How
rdmrbtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { AlertDialog alert = new AlertDialog.Builder(getApplicationContext()).create(); alert.setTitle(No
button = new Button (this); button.setText(Insert values); tableLayout.addView(secondo); button.setOnClickListener(new OnClickListener(){ public void onClick(View arg0)
Button myButton = new Button(this); myButton.setText(Change View); myButton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); myButton.setOnClickListener(new OnClickListener() { public
I have this code : Button groupsButton = (Button)findViewById(R.id.groupsButton); groupsButton.setOnClickListener(new OnClickListener() { public void
public Button stb; static int cnt=0; public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>(); Timer myt;
Hmm.. I am getting an errors saying the constructor is undefined. Dele.setOnClickListener(new OnClickListener() {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.picture_choice_activity); Button takePictureButton = (Button)findViewById(R.id.takePictureButton); takePictureButton.setOnClickListener(new OnClickListener()
int main(void) { char testStr[50] = Hello, world!; char revS[50] = testStr; } I
int* p_bob = new int; *p_bob = 78; The above code makes sense to

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.