我想从其他活动的片段中设置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);更多推荐
发布评论