Reputation: 79
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();
}
}
}
Upvotes: 0
Views: 1566
Reputation: 510
try this :
private void handleChanges() {
Bundle bundle = new Bundle();
bundle.putString("KEY", "string");
FeedBackFragment fragment = new FeedBackFragment();
fragment.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
on your fragment : to get the data :
Bundle bundle = getArguments();
if (bundle != null) {
String data = bundle.getString("KEY");
}
Upvotes: 0
Reputation: 29844
This could happen if you haven't created the fragment in the right way.
If you dynamically add the fragment with:
getSupportFragmentManager().beginTransaction().
replace(R.id.container, new FeedBackFragment(), YOUR_FRAGMENT_TAG).
commit();
You can use the following code to communicate with the fragment.
private void handleChanges() {
FeedBackFragment feedBackFragment =new FeedBackFragment();
// you need to use id if you add the fragment via layout.
//FeedBackFragment feedBackFragment = (FeedBackFragment)
getSupportFragmentManager().findFragmentById(R.id.your_feed_back_fragment_id);
// If you dynamically add the fragment, use tag to find the fragment.
FeedBackFragment feedBackFragment = (FeedBackFragment)
getSupportFragmentManager().findFragmentByTag(YOUR_FRAGMENT_TAG);
if (feedBackFragment != null) {
feedBackFragment.fragmentCommunication(ExtraData);
} else {
Log.i(TAG, "Fragment 2 is not initialized");
}
}
Read more at Creating and Using Fragments.
Upvotes: 1
Reputation: 1010
I have changed some of your code try this. It works for you.
private void handleChanges() {
FeedBackFragment feedBackFragment =new FeedBackFragment();
if (feedBackFragment != null) {
Bundle bundle = new Bundle();
bundle.putString("edttext", "ExtraData");
// set Fragmentclass Arguments
feedBackFragment.setArguments(bundle);
} else {
Log.i(TAG, "Fragment 2 is not initialized");
}
}
And in Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
view = inflater.inflate(R.layout.feedback_fragemnt, container, false);
feedbackEditText = (TextView) view.findViewById(R.id.feedbackEditText);
feedbackEditText.setText(strtext);
return view;
}
Upvotes: 0
Reputation: 330
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);
}
Upvotes: 0
Reputation: 207
http://www.androhub.com/android-pass-data-from-activity-to-fragment/ check this link here you find exact what you want with better clearification
Upvotes: 0