android学习笔记

1、在activity 的 onCreate()方法,主要用于为创建一个activity完成初始化工作,其中

   

tools_text = (TextView) findViewById(R.id.TextView01);
tools_button 
=(Button) findViewById(R.id.Tools_ok);
tools_radiogroup 
= (RadioGroup) findViewById(R.id.RadioGroup01);
setContentView(R.layout.tools);

 将导致tools_text等抛出空指针异常

super.onCreate(stateInstanceState);
setContentView(R.layout.tools);
tools_text 
= (TextView) findViewById(R.id.TextView01);
tools_button 
=(Button) findViewById(R.id.Tools_ok);
tools_radiogroup 
= (RadioGroup) findViewById(R.id.RadioGroup01);

是正确的写法

小结:空指针异常通常与函数的调用顺序有关,注意检查。

原文地址:https://www.cnblogs.com/njj10/p/1902581.html