您的位置:首页 > 移动开发 > Android开发

android新手上路(二)--- 关于findViewById 返回为空

2014-07-29 01:38 351 查看
在oncreate中使用findViewById 时要将findViewById 放在setContentView()之后

因为当调用该函数之后才会绘制当前的layout,之前的layout成员都是不存在的,所以要将findViewById 的调用放在setContentView()之后

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
tv = (TextView)Other.this.findViewById(R.id.text_other);
if(tv != null){
Intent it = getIntent();
tv.setText(it.getStringExtra("enter") + ",终于出来了," + it.getStringExtra("mystring"));
}

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