Reputation: 157
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
Upvotes: 3
Views: 878
Reputation: 576
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);
}
}
}
Upvotes: 1
Reputation: 2795
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);
}
}
Upvotes: 2
Reputation: 5370
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);
Upvotes: 2
Reputation: 73
You can useing store SharedPreferences for save your string and load it in every where.Of course, if you like to change your code.
Upvotes: 2