I want to dynamically hide some elements if a certain condition is met. I use this code to hide a TextView
if (something) {
//do something
} else {
myTextView.setVisibility(View.GONE);
}
I use this code in onCreate.
However, the app breaks on the line where View.GONE is.
Why does it happen? Is it not allowed to hide elements in onCreate? If this is true, where should I use this code?
To clarify, I want to hide them before the Activity loads. The same code in the button’s click event does not break the app.
Your TextView myTextView might be null,
you have to reference it before you use it.
Usually you do this via
As said by Lukas Knuth, check and post your Stracktrace. You should see
a NullPointerException there.