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,81752545
3,81752545
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
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
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