开发者

Why app breaks in onCreate() on line setVisibility(View.GONE)?

I want to d开发者_运维知识库ynamically 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

myTextView = (TextView) findViewById(R.id.mytextviewid)

As said by Lukas Knuth, check and post your Stracktrace. You should see a NullPointerException there.


You can make your view visibility="gone" in your xml.... And check particular condition..

if(cond)
{
myTextViewmyTextView.setVisibility(View.Visible);
}  

Also check id in xml and findViewById() both should be same.... IF myTextView id is not referenced in your code it shows:Null Pointer Exception

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜