如何在不同的布局中获取id

bbk*_*aka 3 android android-layout

id "@+id/call"single_item.xml使用findVewById它时(布局setcontextview(R.layout.main)).应用程序崩溃.如何修复错误

Edd*_*die 36

如果要访问另一个布局(而不是活动布局)中的视图,则可以使您想要使用的布局膨胀并以这种方式访问​​它.

例:

View inflatedView = getLayoutInflater().inflate(R.layout.other_layout, null);
TextView text = (TextView) inflatedView.findViewById(R.id.text_view);
text.setText("Hello!");
Run Code Online (Sandbox Code Playgroud)

有关充气布局的更多信息,请点击此处.


nba*_*lle 0

您试图找到在 R.layout.main 布局中的 R.layout.single_item 中声明的视图(R.id.call),所以我猜它会抛出空指针异常。

您应该在 main.xml 文件中声明“@+id/call”元素,或者将上下文视图设置为 R.layout.single_item