Android未读取布局文件而意外退出问题。
前几日刚开始Android开发,照着书本敲代码后调试没有报错,但是在运行中的程序出现了 【应用程序意外停止,请重试】的问题。
原代码如下:
代码
1 public void onCreate(Bundle savedInstanceState)
2 {
3 super.onCreate(savedInstanceState);
4 textview = (TextView) this.findViewById(R.id.tv);
5 setContentView(R.layout.main);
6
7 Button btnOK = (Button) findViewById(R.id.btnOK);
2 {
3 super.onCreate(savedInstanceState);
4 textview = (TextView) this.findViewById(R.id.tv);
5 setContentView(R.layout.main);
6
7 Button btnOK = (Button) findViewById(R.id.btnOK);
8 ……
分析问题,发现在第4行时就报错,监视textview值为null……
问题原因,在第4行时,尚未找到R.id.tv这个资源,因为系统还没有读到布局文件……
解决方法:4行和5行对调即可……就是必须先读取布局文件~~
希望对跟我一样的菜鸟有所帮助~