Hello everyone i need help in passing data from activity to fragment.
im using the this way but getting error of null pointer .
In main Activity
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.leftfeedback:
handleChanges();
break;
}
}
private void handleChanges() {
FeedBackFragment feedBackFragment =new FeedBackFragment();
if (feedBackFragment != null) {
feedBackFragment.fragmentCommunication(ExtraData);
} else {
Log.i(TAG, "Fragment 2 is not initialized");
}
}
in fragment side all given data is coming i checked with log before to set on
public class FeedBackFragment extends Fragment{
private static final String TAG ="FeedBackFragment" ;
View view;
TextView feedbackEditText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.feedback_fragemnt, container, false);
feedbackEditText = (TextView) view.findViewById(R.id.feedbackEditText);
return view;
}
public void fragmentCommunication(String feedBackData) {
log.i(TAG,feedBackData);
try {
JSONObject jsonObject = new JSONObject(feedBackData);
String message = jsonObject.getString("message");
if(message.trim()!=null){
feedbackEditText.setText(message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Because of feedBackFragment
not create, so Edittext is null
you should attach your feedBackFragment to MainActivity.
FeedBackFragment feedBackFragment;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.leftfeedback:
handleChanges();
break;
}
}
private void handleChanges() {
if (null == feedBackFragment) {
feedBackFragment = new FeedBackFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(contentId, feedBackFragment);
transaction.commit();
}
feedBackFragment.fragmentCommunication(ExtraData);
}