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
您可以使用存储SharedPreferences来保存字符串并将其加载到每个位置。当然,如果您想更改代码。
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);
No need to findView in setText method you should do in this way and it will work
public class FragmentA extends Fragment {
TextView valueName,valueStatus ;
@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);
return layout;
}
public void setText(String name, String status){
valueName.setText(name);
valueStatus.setText(status);
}
}
Try to do like this and make sure your fragment is attached with activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grupos);
if(savedInstanceState == null)
{
fragment = new FragmentA();
fragment.setTag(R.id.myfragmentId);
getFragmentManager().beginTransaction()
.add(R.id.container, fragment).commit();
}
else
{
if(fragment == null)
{
fragment = (FragmentA) getFragmentManager().findFragmentByTag(R.id.myfragmentId);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.