gpt4 book ai didi

android - 从 fragment B 中的 fragment A 访问 View

转载 作者:行者123 更新时间:2023-11-30 02:28:36 26 4
gpt4 key购买 nike

我试图从 fragment B 中的 fragment A 访问 Textview 以更改文本,我得到一个空指针。我试过以下。

public void setText(String text) {
TextView t = (TextView) getView().findViewById(R.id.message_menu);
t.setText(文本);
}

我也试过

 test = (TextView) getView().findViewById(R.id.message_menu);
test.setText("bla bla");

我也试过膨胀另一个 View

View myFragmentView = inflater.inflate(R.layout.fragment_login, container, false);
test = (TextView) test..findViewById(R.id.message_menu);

尝试在 fragment B 中使用具有以下代码的 NewInstance

test = (TextView) FragmentLogin.newInstance().getView().findViewById(R.id.TEXT_STATUS_ID);
test.setText("hello testing ");

fragment A:

 public static FragmentBottomMenu newInstance() {
FragmentBottomMenu fragment = new FragmentBottomMenu();
return fragment;
}

仍然得到一个空指针异常。

任何建议都会很棒。谢谢

最佳答案

您的代码示例指向当前 fragment 的 View !要正确访问其他 fragment 的 View ,请使用:

text = (TextView) [otherFragmentInstance].getView().findViewById(R.id.message_menu);
text.setText("bla bla");

关于android - 从 fragment B 中的 fragment A 访问 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27593694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com