1

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)

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
  • you need to implement interface in activity then check if fragment is loaded then call the fragment method. – Saurabh Bhandari Nov 10 '18 at 09: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 '18 at 09: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 '18 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 '18 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 '18 at 10:19
  • @SaurabhBhandari I just implement the interface in activity. Then what happen is it is not working. – Muhammad Zawawi Nov 10 '18 at 11:20
  • What @SaurabhBhandari is saying it's correct (at least one of the possible approaches). Activity start -> FeedFragment talk with -> Activity decide what to do (in your case Activity will replace FeedFragment with Transfer Fragment passing the value received as arguments in the static constructor of Transfer). I'll try to add some code if this helps you – MatPag Nov 10 '18 at 11:22
  • @MatPag yes..I had done it like this example https://inducesmile.com/android/android-fragment-how-to-pass-data-between-fragments-in-an-activity/. It uses interface and in activity in implement interface. But when I do it in navigation drawer in not gonna work. – Muhammad Zawawi Nov 10 '18 at 11:24
  • You need to add your not working code (drawer too). I've done it plenty of times, you are probably doing something wrong in your implementation – MatPag Nov 10 '18 at 11:26
  • @MuhammadZawawi I've followed this simple tutorial for Navigation Drawer and Fragments togheter long time ago: https://guides.codepath.com/android/fragment-navigation-drawer It's perfect for a basic start – MatPag Nov 10 '18 at 11:31
  • @MatPag you can check my code in MainActivity here https://gist.github.com/zawawimanja/960b0eb72bffbacc29f4d0c968df5f1e. I just add implement and method from class of interface name sendInput. I also try other project using viewpager and try changing the findfragmentbyid or findfragmentbytag by declaring public static String tag also not work. The project that I mentioned is here https://www.journaldev.com/14207/android-passing-data-between-fragments. – Muhammad Zawawi Nov 10 '18 at 11:33
  • @MatPag the link that you mentioned is not related with data transfering. I dont have any problem about navigate from fragment to fragment. – Muhammad Zawawi Nov 10 '18 at 11:40
  • @SaurabhBhandari this is my gist for MainActivity. You can check it . I have done it like you ask. gist.github.com/zawawimanja/960b0eb72bffbacc29f4d0c968df5f1e – Muhammad Zawawi Nov 10 '18 at 11:41
  • You have both the Fragments visible at the same time? – MatPag Nov 10 '18 at 11:46
  • You try to do this: `Transfer secondFragment = (Transfer) getSupportFragmentManager().findFragmentById(R.id.transfer); secondFragment.send(input);` while your Transfer fragment is not on the container. You should create the Transfer Fragment in the `sendInput` method. Something like: `Transfer fragment = Transfer.newInstance(input); getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();` – MatPag Nov 10 '18 at 11:53
  • On How to implement Transfer.newInstance(input), read here: https://stackoverflow.com/a/9245510/2910520 – MatPag Nov 10 '18 at 11:54
  • @MatPag both fragment visible when i open it . I can only know it will visible at the same time if i put both fragmentlayout in mainactivity layout. For the new instance technique it using bundle. The last time I use bundle there will an error. Maybe the last choice, I will revert to use activity. Anyway Thank You ! – Muhammad Zawawi Nov 10 '18 at 12:26

0 Answers0