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
|
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
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
2 days ago
@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
2 days ago
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
2 days ago
@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
2 days ago
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
2 days ago
|
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
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
java
edited 2 days ago
Mikhail Kholodkov
3,48842141
3,48842141
asked 2 days ago
Muhammad Zawawi
67
67
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
2 days ago
@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
2 days ago
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
2 days ago
@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
2 days ago
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
2 days ago
|
show 12 more comments
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
2 days ago
@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
2 days ago
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
2 days ago
@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
2 days ago
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
2 days ago
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
2 days ago
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
2 days ago
@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
2 days ago
@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
2 days ago
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
2 days ago
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
2 days ago
@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
2 days ago
@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
2 days ago
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
2 days ago
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
2 days ago
|
show 12 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
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
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
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
you need to implement interface in activity then check if fragment is loaded then call the fragment method.
– Saurabh Bhandari
2 days ago
@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
2 days ago
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
2 days ago
@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
2 days ago
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
2 days ago