Android的TextView的和空指针异常 [英] Android TextView and null pointer exception
本文介绍了Android的TextView的和空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么
TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();
生成一个空指针异常?该ID是正确的,testTextView在我的XML布局文件被正确声明的。
generating a null pointer exception? The id is correct, testTextView is correctly declared in my XML layout file.
推荐答案
为 findViewById
返回空值,如果你通过一个有效ID的唯一原因是,你要么设置错误的内容预览(的setContentView
)或不设置内容视图都没有。
The only reason for findViewById
to return null if you are passing a valid id is that you are either setting the wrong content view (with setContentView
) or not setting a content view at all.
这篇关于Android的TextView的和空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文