手动转换 set.id() 后无法访问合成



我有一个小问题,你们能帮我吗?

我有一个像这样的代码:

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,但是您需要检查这个链接,

最新更新