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 8108881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:14:52+00:00 2026-06-06T01:14:52+00:00

I have defined the TextView array as final. And I set a OnClickListener() for

  • 0

I have defined the TextView array as final.
And I set a OnClickListener() for all the array elements,
and I am able to use setBackground (even the array is not set as final).
But I cannot use getText() method which return NullPointerException during operation.

Do you guys know why is the problem existed?

final ArrayList<TextView> seats = new ArrayList<TextView>();

@Override
public void onCreate(Bundle savedInstanceState) {
    .
    .
    seats.add((TextView) findViewById(R.id.A1));
    seats.add((TextView) findViewById(R.id.A2));
    seats.add((TextView) findViewById(R.id.A3));
    .
    .
    for (int i = 0; i<seats.size(); i++)
        seats.get(i).setOnClickListener(seatClick);
    .
    .
}
OnClickListener seatClick = new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        v.setBackgroundColor(-16711681);
        Toast.makeText(getApplicationContext(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT);
    }
};

The TextView is defined something like this:

<TextView
        android:id="@+id/A1"
        android:background="#0AFC1A"
        android:textColor="#FFFFFF"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="A1" />
  • 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-06-06T01:14:53+00:00Added an answer on June 6, 2026 at 1:14 am

    i tested your code its working fine. just change it.

     Toast.makeText(getApplicationContext(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT).show();
    

    this is full code

    seats.add((TextView) findViewById(R.id.textView2));
                seats.add((TextView) findViewById(R.id.textView3));
                seats.add((TextView) findViewById(R.id.textView4));
    
    
    
                OnClickListener seatClick = new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                      v.setBackgroundColor(-16711681);
                          Toast.makeText(getApplicationContext(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                };
    
                for (int i = 0; i<seats.size(); i++)
                    seats.get(i).setOnClickListener(seatClick);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined CSS in MasterPage so that all pages can use the same
I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:
I have the following TextView defined: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtCredits" android:autoLink="web" android:id="@+id/infoTxtCredits" android:layout_centerInParent="true" android:linksClickable="true"/>
I have a view in the XML <TextView style=@style/TitleBarText /> whose styles are defined
ok so I have an ExpandableListView, I have defined all my views and everything
So I have a custom view set up in code (no XML layout defined
I have an XML layout that defines a TextView box 50px x 320px who
I have a simple TextView and I want it to automatically define the font
I have defined following control template for my custom control. <ControlTemplate TargetType={x:Type local:CustomControl}> <Grid
I have defined a ConfigurationProperty class, which is basicly a key-value pair (with key

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.