user631063
user631063

Reputation:

Go through XML and set all TextView to be invisible

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!

Upvotes: 2

Views: 3244

Answers (3)

Kevin Coppock
Kevin Coppock

Reputation: 134664

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. :)

Upvotes: 6

Nathan Fig
Nathan Fig

Reputation: 15109

As the others have indicated, the most obvious cause of a NullPointerException would be that you're not checking if text is null before calling setVisibility(). You'll want to look into why text would be null in the first place- but you should check your pointers regardless.

Or just go with kcoppock's alternative.

Any particular reason you're using setVisibility? It isn't always appropriate- I believe all it does is keep draw from being called, which can cause issues later if you hoped to treat it as drawn but invisible.

Upvotes: 0

hgllnt
hgllnt

Reputation: 338

Did you do some debugging? For instance, have a look if the resIDs match. At least they're not zero, if your error occurs right there. (Have you checked that?).

It may sound odd, but you could also check if getResources() and this.findViewById() refer to the same object.

That's all I can think of for the moment.

Upvotes: 0

Related Questions