Android TextView和空指针异常

SK9*_*SK9 6 android textview

为什么是

TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();
Run Code Online (Sandbox Code Playgroud)

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

Cri*_*ian 27

findViewById如果传递有效ID,则返回null 的唯一原因是您要么设置错误的内容视图(带setContentView),要么根本不设置内容视图.