Android: Can't Update Textview In Fragment From Activity. Nullpointerexception
I'm trying to update textview in fragment from another activity. But I'm getting a NUllPointerException when calling the setText method. I have tried the following, but still getti
Solution 1:
Use below callback:
Fragment Class:
publicclassFragmentOneextendsFragment {
private ViewCallback mCallback;
publicFragmentOne(ViewCallback mCallback) {
this.mCallback = mCallback;
}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_one, container, false);
mCallback.updateTextView((TextView) view.findViewById(R.id.fragmentTextView));
return view;
}
publicinterfaceViewCallback {
voidupdateTextView(TextView view);
}
}
Below is the Activity class:
publicclassMainCallbackActivityextendsActivityimplementsCallbackFragment.ViewCallback {
publicTextView textView;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_callback);
FragmentOne fragment = newFragmentOne(this);
getFragmentManager().beginTransaction().add(R.id.frameLayout, fragment).commit();
}
@OverrideprotectedvoidonResume() {
super.onResume();
if (textView != null)
textView.setText("Updating Fragment TextView in Activity..!!");
}
@OverridepublicvoidupdateTextView(TextView view) {
this.textView = view;
}
}
Implment that call back in your activity class..then update the textview.
Post a Comment for "Android: Can't Update Textview In Fragment From Activity. Nullpointerexception"