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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:12:51+00:00 2026-05-20T10:12:51+00:00

Hey this is my first time trying anything like so I don’t know if

  • 0

Hey this is my first time trying anything like so I don’t know if this is even close to the best way to do this, but I thought it would work. I’m trying to go through the XML layout file and set all TextView‘s to be INVISIBLE. When the following method is called I get a NullPointerException

    public void numPlayerSetup(){
         {
             for(int i = 3; i <= 6; i++) 
                    for(int z = 2; z <= 10; z++){
                    int resID = getResources().getIdentifier("TextView"+Integer.toString(z) + Integer.toString(i), "id", this.getPackageName());
                    if(resID != 0){
                        TextView text = (TextView) this.findViewById(resID);
                        text.setVisibility(View.INVISIBLE);
                    }
        }

Let me know if you have any suggestions. Thanks!

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

    Well, are the ids going to change? If not, just set up an int[] of TextView IDs, and loop through those, e.g.:

    int[] ids = {
        R.id.tv1, R.id.tv2, R.id.tv3 //...
    }
    
    for(int i : ids) {
        TextView tv = (TextView)findViewById(i);
        tv.setVisibility(View.INVISIBLE);
    }
    

    I would definitely not try using reflection, it’d be a lot less efficient than doing it in other ways. If you don’t know the IDs of the TextViews ahead of time, why not try something like this (assuming your root layout is a RelativeLayout):

    RelativeLayout root = (RelativeLayout)findViewById(R.id.root);
    for(int i = 0; i < root.getChildCount(); i++) {
    View v = findViewById(i);
    if(v instanceof TextView) {
    ((TextView)v).setVisibility(View.INVISIBLE);
    }
    }

    Since you’ve already accepted, I’ll assume method 1 worked, because I just realized I was horribly off on method 2. It should be getChildAt(i), not findViewById(i), as that would just be calling findViewById(0|1|2|...etc). Below is a corrected version:

    RelativeLayout root = (RelativeLayout)findViewById(R.id.root);
    for(int i = 0; i < root.getChildCount(); i++) {
        View v = root.getChildAt(i);
        if(v instanceof TextView) {
            ((TextView)v).setVisibility(View.INVISIBLE);
        }
    }
    

    I haven’t tested that, but it sounds good in theory. 🙂

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

Sidebar

Related Questions

Hey guys, this is my first time actually posting at stackflow but i've used
Hey Everyone. I'm a first time poster, but I've browsed this site a number
Hey There, This is a pretty simple problem, but it is my first time
Hey everyone, my first time trying to draw a multi-color gradient in actionscript 3.
Hey everyone.. This is my first time writing an ASP.NET MVC web app, and
Sometimes Eclipse comes up saying hey you should debug this line!!! but doesn't actually
I have a string of test like this: <customtag>hey</customtag> I want to use a
Hey Overflow. Long time listener, first time caller. So here's what I've got: I'm
Hey all, i have a problem with trying to get 2 forms to close
Hey, I'm pretty interested in OS writing, I was for a long time, but

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.