您的位置:首页 > 其它

findViewById如果找不到控件

2015-06-27 19:49 351 查看
这不是什么高级的话题, 只是写在这里提醒初学者们, 不要犯这样的错误… 如果你的findViewById找不到想要的控件, 但是该控件的
id
等配置都没有任何错误, 那么请看看获取控件的语句, 是不是写在了

setContentView(R.layout.activity_reminder_edit);

这句话的前面…

在没有
setContentView
的情况下, 直接通过

super.onCreate(savedInstanceState);

mDateButton = (Button) this.findViewById(R.id.reminder_date);
mTimeButton = (Button) this.findViewById(R.id.reminder_time);
confirm = (Button)this.findViewById(R.id.confirm);
mCalendar = Calendar.getInstance();

setContentView(R.layout.activity_reminder_edit); //在设置ContentView之前尝试获取控件


来获取控件, 那么就这么悲剧了…



一串的
null
, 接下来就准备接收空指针错误吧!

KEEP CALM AND CARRY ON
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: