Does the Textview.gettext return null?

时间:2016-04-04 18:01:15

标签: android null textview

In the following code,

TextView textv = (TextView)findViewById(R.id.textview);

when I call textv.getText(); while the textview has nothing and is just declared does it returns a charsequence(not null) of what ? I don't get it

2 个答案:

答案 0 :(得分:1)

getText() will not return null, it will return an empty string if there is no string written in, which is definitely not null

答案 1 :(得分:1)

您可以先调试,看看是否在您的活动/片段中找到了textview。 尝试if(texttv!= null)并查看它显示的内容。很可能在您的xml文件中找不到它。 您也可以在初始化后立即尝试设置虚拟文本。 textv.setText(“按代码设置的文本”); 如果您在textview中看到文本,那么您将知道它是否已分配。 希望它有所帮助!