Android TextView 和空指针异常 [英] Android TextView and null pointer exception

查看:25
本文介绍了Android TextView 和空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();

产生空指针异常?id 正确,在我的 XML 布局文件中正确声明了 testTextView.

generating a null pointer exception? The id is correct, testTextView is correctly declared in my XML layout file.

推荐答案

findViewById 在传递有效 id 时返回 null 的唯一原因是您设置了错误的内容视图(使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆