我有一个小问题,你们能帮我吗?
我有一个像这样的代码:
TextView tz;
TextView tv = new TextView(this);
tv.setId(0x7f090024);
tv.setText("hello");
View vv = findViewById(0x7f090024);
if (vv != null)
{
Log.e("number_id", "not null");
}
else
Log.e("number_id", "null");
// also return error (java lang null pointer exception) with this
tz = (TextView) findViewById(0x7f090024);
Log.e("test", tz.getText().toString());
为什么这个结果总是返回null
,而我不能从第二个文本字段tz
中获得值?
它返回null,因为findViewById
引用setContentView
构建的View
层次结构。您的TextView
不属于它,所以您不能用findViewById
检索它。
是的,您可以setID,您可以通过获取id访问widget,但是您需要检查这个链接,