来自与片段无关的另一活动的片段中的setText(setText in fragment from another activity not related with fragment)

编程入门 行业动态 更新时间:2024-10-28 18:24:06
来自与片段无关的另一活动的片段中的setText(setText in fragment from another activity not related with fragment)

我想从其他活动的片段中设置textView ,该活动不是MainActivity片段transaction 。

已经尝试了一些与我的问题有关的其他相关文章的方法,但是得到了一个错误..

这里是我的方法片段从其他活动recieve

片段A

public class FragmentA extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ProgressDialog pDialog = new ProgressDialog(getContext()); pDialog.setCancelable(false); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflating view layout View layout = inflater.inflate(R.layout.fragment_A, container, false); //Put Data to id fragment valueName = (TextView) layout.findViewById(R.id.valueNameNav); valueStatus = (TextView) layout.findViewById(R.id.valueStatusNav); } public void setText(String name, String status){ valueName = (TextView) getView().findViewById(R.id.valueNameNav); valueName.setText(name); valueStatus = (TextView) getView().findViewById(R.id.valueStatusNav); valueStatus.setText(status); } }

这就是我如何在活动片段中调用setText方法

String editValueName= editName.getText().toString(); String lastStatus = valueStatus.getText().toString(); FragmentA mFragment = (FragmentA ) getSupportFragmentManager().findFragmentById(R.id.fragment_A); mFragment.setText(editValueName, lastStatus);

但有这样的错误

java.lang.NullPointerException:试图在空对象引用上调用虚拟方法'void com.example.study.fragment.fragmentA.setText(java.lang.String,java.lang.String)'

100%确定字符串getText上有一个数据字符串

i wanna set textView in fragment from another activity which this activity is not MainActivity has fragment transaction ..

already tried some method from the other related article which related with my problem, but got an error..

here's my method inside fragment to recieve from another activity

Fragment A

public class FragmentA extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ProgressDialog pDialog = new ProgressDialog(getContext()); pDialog.setCancelable(false); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflating view layout View layout = inflater.inflate(R.layout.fragment_A, container, false); //Put Data to id fragment valueName = (TextView) layout.findViewById(R.id.valueNameNav); valueStatus = (TextView) layout.findViewById(R.id.valueStatusNav); } public void setText(String name, String status){ valueName = (TextView) getView().findViewById(R.id.valueNameNav); valueName.setText(name); valueStatus = (TextView) getView().findViewById(R.id.valueStatusNav); valueStatus.setText(status); } }

and this is how i call setText method in fragment from activity

String editValueName= editName.getText().toString(); String lastStatus = valueStatus.getText().toString(); FragmentA mFragment = (FragmentA ) getSupportFragmentManager().findFragmentById(R.id.fragment_A); mFragment.setText(editValueName, lastStatus);

but got an error like this

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.study.fragment.fragmentA.setText(java.lang.String, java.lang.String)' on a null object reference

100% sure there's a data string on string getText

最满意答案

在你的活动中创建一个FrameLayout,使用高度宽度为id的container为MATCH_PARENT然后像这样在你的活动中添加片段

FragmentA newFragment = new FragmentA (); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.container, newFragment).commit();

并设置你的文字

String editValueName= editName.getText().toString(); String lastStatus = valueStatus.getText().toString(); newFragment .setText(editValueName, lastStatus);

Create a FrameLayout in your activity with id container with height width to MATCH_PARENT Then add fragment in your activity like this

FragmentA newFragment = new FragmentA (); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.container, newFragment).commit();

and than set your text

String editValueName= editName.getText().toString(); String lastStatus = valueStatus.getText().toString(); newFragment .setText(editValueName, lastStatus);

更多推荐

本文发布于:2023-07-22 02:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1216286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   setText   fragment   activity   related

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!