加载中…
个人资料
灵思迈
灵思迈
  • 博客等级:
  • 博客积分:0
  • 博客访问:4,904
  • 关注人气:161
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

安卓setText(java.lang.CharSequence) on a null object reference解决办法

(2017-11-29 21:16:28)
标签:

it


Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(' on a null object reference


故障出现的现象:

在一个Fragment的前面声明了全局变量 

private TextView textViewTodayTemp;
在onActivityCreated()

却又声明了局部变量并关联:

TextView  textViewTodayTemp = ( TextView)getActivity().findViewById(R.id.textViewTodayTemp);
然后在别的方法中调用:
textViewTodayTemp.setText()
导致出现错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(' on a null object reference

故障解决办法:

onActivityCreated()中,不用声明局部变量,去掉前面的TextView,改为:

 textViewTodayTemp = ( TextView)getActivity().findViewById(R.id.textViewTodayTemp);

这样故障解决。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有