Problem Passing data between Fragment to Fragment using Navigation Drawer
up vote
1
down vote
favorite
I have 2 fragment consist of FeedFragment
(Fragment) which will transfer the string to Transfer
(Fragment). I am using interface to transfer it.
FeedFragment
public class FeedFragment extends Fragment
public interface OnInputSelected
void sendInput(String input);
public OnInputSelected mOnInputSelected;
private TextView feed;
private TextView bt;
private String input;
public FeedFragment()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_feed, container, false);
feed= view.findViewById(R.id.feed);
bt= view.findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
input=feed.getText().toString();
mOnInputSelected.sendInput(input);
);
// Inflate the layout for this fragment
return view;
@Override
public void onAttach(Context context)
super.onAttach(context);
try
mOnInputSelected = (FeedFragment.OnInputSelected) getTargetFragment();
catch (ClassCastException e)
Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage());
Transfer
public class Transfer extends Fragment implements FeedFragment.OnInputSelected
public TextView transfer;
@Override
public void sendInput(String input)
transfer.setText(input);
public Transfer()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_transfer, container, false);
// Inflate the layout for this fragment
transfer= view.findViewById(R.id.transfer);
return view;
Error
Attempt to invoke interface method 'void com.shrikanthravi.customnavigationdrawer.FeedFragment$OnInputSelected.sendInput(java.lang.String)' on a null object reference
at com.shrikanthravi.customnavigationdrawer.FeedFragment$1.onClick(FeedFragment.java:49)
java android null
|
show 12 more comments
up vote
1
down vote
favorite
I have 2 fragment consist of FeedFragment
(Fragment) which will transfer the string to Transfer
(Fragment). I am using interface to transfer it.
FeedFragment
public class FeedFragment extends Fragment
public interface OnInputSelected
void sendInput(String input);
public OnInputSelected mOnInputSelected;
private TextView feed;
private TextView bt;
private String input;
public FeedFragment()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_feed, container, false);
feed= view.findViewById(R.id.feed);
bt= view.findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
input=feed.getText().toString();
mOnInputSelected.sendInput(input);
);
// Inflate the layout for this fragment
return view;
@Override
public void onAttach(Context context)
super.onAttach(context);
try
mOnInputSelected = (FeedFragment.OnInputSelected) getTargetFragment();
catch (ClassCastException e)
Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage());
Transfer
public class Transfer extends Fragment implements FeedFragment.OnInputSelected
public TextView transfer;
@Override
public void sendInput(String input)
transfer.setText(input);
public Transfer()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_transfer, container, false);
// Inflate the layout for this fragment
transfer= view.findViewById(R.id.transfer);
return view;
Error
Attempt to invoke interface method 'void com.shrikanthravi.customnavigationdrawer.FeedFragment$OnInputSelected.sendInput(java.lang.String)' on a null object reference
at com.shrikanthravi.customnavigationdrawer.FeedFragment$1.onClick(FeedFragment.java:49)
java android null
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
Nov 10 at 9:53
@SaurabhBhandari why do i need to implement interface in activity? I am not using the interface in activity. I am using it in Transfer(fragment).
– Muhammad Zawawi
Nov 10 at 9:55
you have to implement in activity because if your fragment loaded or not you can check in activity. if your fragment already added then simply call your fragment method or if not loaded crate new instance and then call data
– Saurabh Bhandari
Nov 10 at 10:01
@SaurabhBhandari i dont think it will work. Because I already tried implement interface in activity and surely it will get the data from fragment. But know the point is to transfer the data from fragment to fragment. Not fragment to activity .
– Muhammad Zawawi
Nov 10 at 10:16
you can directly transfer data one fragment to another if you are directly adding fragment from current fragment otherwise you have to pass data through activity .
– Saurabh Bhandari
Nov 10 at 10:19
|
show 12 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have 2 fragment consist of FeedFragment
(Fragment) which will transfer the string to Transfer
(Fragment). I am using interface to transfer it.
FeedFragment
public class FeedFragment extends Fragment
public interface OnInputSelected
void sendInput(String input);
public OnInputSelected mOnInputSelected;
private TextView feed;
private TextView bt;
private String input;
public FeedFragment()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_feed, container, false);
feed= view.findViewById(R.id.feed);
bt= view.findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
input=feed.getText().toString();
mOnInputSelected.sendInput(input);
);
// Inflate the layout for this fragment
return view;
@Override
public void onAttach(Context context)
super.onAttach(context);
try
mOnInputSelected = (FeedFragment.OnInputSelected) getTargetFragment();
catch (ClassCastException e)
Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage());
Transfer
public class Transfer extends Fragment implements FeedFragment.OnInputSelected
public TextView transfer;
@Override
public void sendInput(String input)
transfer.setText(input);
public Transfer()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_transfer, container, false);
// Inflate the layout for this fragment
transfer= view.findViewById(R.id.transfer);
return view;
Error
Attempt to invoke interface method 'void com.shrikanthravi.customnavigationdrawer.FeedFragment$OnInputSelected.sendInput(java.lang.String)' on a null object reference
at com.shrikanthravi.customnavigationdrawer.FeedFragment$1.onClick(FeedFragment.java:49)
java android null
I have 2 fragment consist of FeedFragment
(Fragment) which will transfer the string to Transfer
(Fragment). I am using interface to transfer it.
FeedFragment
public class FeedFragment extends Fragment
public interface OnInputSelected
void sendInput(String input);
public OnInputSelected mOnInputSelected;
private TextView feed;
private TextView bt;
private String input;
public FeedFragment()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_feed, container, false);
feed= view.findViewById(R.id.feed);
bt= view.findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
input=feed.getText().toString();
mOnInputSelected.sendInput(input);
);
// Inflate the layout for this fragment
return view;
@Override
public void onAttach(Context context)
super.onAttach(context);
try
mOnInputSelected = (FeedFragment.OnInputSelected) getTargetFragment();
catch (ClassCastException e)
Log.e(TAG, "onAttach: ClassCastException : " + e.getMessage());
Transfer
public class Transfer extends Fragment implements FeedFragment.OnInputSelected
public TextView transfer;
@Override
public void sendInput(String input)
transfer.setText(input);
public Transfer()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_transfer, container, false);
// Inflate the layout for this fragment
transfer= view.findViewById(R.id.transfer);
return view;
Error
Attempt to invoke interface method 'void com.shrikanthravi.customnavigationdrawer.FeedFragment$OnInputSelected.sendInput(java.lang.String)' on a null object reference
at com.shrikanthravi.customnavigationdrawer.FeedFragment$1.onClick(FeedFragment.java:49)
java android null
java android null
edited Nov 10 at 10:34
Mikhail Kholodkov
3,70252343
3,70252343
asked Nov 10 at 9:07
Muhammad Zawawi
67
67
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
Nov 10 at 9:53
@SaurabhBhandari why do i need to implement interface in activity? I am not using the interface in activity. I am using it in Transfer(fragment).
– Muhammad Zawawi
Nov 10 at 9:55
you have to implement in activity because if your fragment loaded or not you can check in activity. if your fragment already added then simply call your fragment method or if not loaded crate new instance and then call data
– Saurabh Bhandari
Nov 10 at 10:01
@SaurabhBhandari i dont think it will work. Because I already tried implement interface in activity and surely it will get the data from fragment. But know the point is to transfer the data from fragment to fragment. Not fragment to activity .
– Muhammad Zawawi
Nov 10 at 10:16
you can directly transfer data one fragment to another if you are directly adding fragment from current fragment otherwise you have to pass data through activity .
– Saurabh Bhandari
Nov 10 at 10:19
|
show 12 more comments
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
Nov 10 at 9:53
@SaurabhBhandari why do i need to implement interface in activity? I am not using the interface in activity. I am using it in Transfer(fragment).
– Muhammad Zawawi
Nov 10 at 9:55
you have to implement in activity because if your fragment loaded or not you can check in activity. if your fragment already added then simply call your fragment method or if not loaded crate new instance and then call data
– Saurabh Bhandari
Nov 10 at 10:01
@SaurabhBhandari i dont think it will work. Because I already tried implement interface in activity and surely it will get the data from fragment. But know the point is to transfer the data from fragment to fragment. Not fragment to activity .
– Muhammad Zawawi
Nov 10 at 10:16
you can directly transfer data one fragment to another if you are directly adding fragment from current fragment otherwise you have to pass data through activity .
– Saurabh Bhandari
Nov 10 at 10:19
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
Nov 10 at 9:53
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
Nov 10 at 9:53
@SaurabhBhandari why do i need to implement interface in activity? I am not using the interface in activity. I am using it in Transfer(fragment).
– Muhammad Zawawi
Nov 10 at 9:55
@SaurabhBhandari why do i need to implement interface in activity? I am not using the interface in activity. I am using it in Transfer(fragment).
– Muhammad Zawawi
Nov 10 at 9:55
you have to implement in activity because if your fragment loaded or not you can check in activity. if your fragment already added then simply call your fragment method or if not loaded crate new instance and then call data
– Saurabh Bhandari
Nov 10 at 10:01
you have to implement in activity because if your fragment loaded or not you can check in activity. if your fragment already added then simply call your fragment method or if not loaded crate new instance and then call data
– Saurabh Bhandari
Nov 10 at 10:01
@SaurabhBhandari i dont think it will work. Because I already tried implement interface in activity and surely it will get the data from fragment. But know the point is to transfer the data from fragment to fragment. Not fragment to activity .
– Muhammad Zawawi
Nov 10 at 10:16
@SaurabhBhandari i dont think it will work. Because I already tried implement interface in activity and surely it will get the data from fragment. But know the point is to transfer the data from fragment to fragment. Not fragment to activity .
– Muhammad Zawawi
Nov 10 at 10:16
you can directly transfer data one fragment to another if you are directly adding fragment from current fragment otherwise you have to pass data through activity .
– Saurabh Bhandari
Nov 10 at 10:19
you can directly transfer data one fragment to another if you are directly adding fragment from current fragment otherwise you have to pass data through activity .
– Saurabh Bhandari
Nov 10 at 10:19
|
show 12 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237491%2fproblem-passing-data-between-fragment-to-fragment-using-navigation-drawer%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
Nov 10 at 9:53
@SaurabhBhandari why do i need to implement interface in activity? I am not using the interface in activity. I am using it in Transfer(fragment).
– Muhammad Zawawi
Nov 10 at 9:55
you have to implement in activity because if your fragment loaded or not you can check in activity. if your fragment already added then simply call your fragment method or if not loaded crate new instance and then call data
– Saurabh Bhandari
Nov 10 at 10:01
@SaurabhBhandari i dont think it will work. Because I already tried implement interface in activity and surely it will get the data from fragment. But know the point is to transfer the data from fragment to fragment. Not fragment to activity .
– Muhammad Zawawi
Nov 10 at 10:16
you can directly transfer data one fragment to another if you are directly adding fragment from current fragment otherwise you have to pass data through activity .
– Saurabh Bhandari
Nov 10 at 10:19