2

I'm trying to write method which is sending data from fragment to another fragment in Android Studio. I've got sample app where this method is created already. And it's working fine.

I just duplicate every method and the interface exactly the same as in the sample app and create a new fragment class (which is also the same as in sample).

The MainActivity looks like this 
public class MainActivity extends AppCompatActivity implements FragmentOne.SendMessage, FragmentOne.SendMessageLanLon {

    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void sendData(String message) {
        String tag = "android:switcher:" + R.id.viewPager + ":" + 1;
        FragmentTwo f = (FragmentTwo) getSupportFragmentManager().findFragmentByTag(tag);
        f.displayReceivedData(message);
    }

    @Override
    public void sendDataLatLon(String message) {
        String tag = "android:switcher:" + R.id.viewPager + ":" + 2;
        FragmentThree f = (FragmentThree) getSupportFragmentManager().findFragmentByTag(tag);
        f.displayReceivedDataLanLon(message);
    }
}

sendData is working correctly but sendDataLatLon crashes - I've got a message

  java.lang.NullPointerException: Attempt to invoke virtual method 'void com.journaldev.passingdatabetweenfragments.FragmentThree.displayReceivedDataLanLon(java.lang.String)' on a null object reference
                                                     at com.journaldev.passingdatabetweenfragments.MainActivity.sendDataLatLon(MainActivity.java:38)
                                                     at com.journaldev.passingdatabetweenfragments.FragmentOne$1.onClick(FragmentOne.java:87)

Now in FragmentOne - I'm using button - everything's working alright

 bttnPass.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    SM.sendData(inDataLat.toString().trim() + ";" + inDataLon.toString().trim());
                }

            });

In the same fragment, when I'm using another button (same as above) there's an error in line SLL.sendData which you can see in logcat above.

Here's the rest of the code which I wrote

interface SendMessage {
        void sendData(String message);

    }

    interface SendMessageLanLon {
        void sendDataLatLon(String message);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            SM = (SendMessage) getActivity();
            SLL = (SendMessageLanLon) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException("Error in retrieving data. Please try again");
        }
    }

Now, referring to a log I don't know why a object is null. The message that I'm sending is not null and the method is the SAME as the working one.

Is there's something wrong with a tag value in my method? Here's the ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        if (position == 0) {
            fragment = new FragmentOne();
        } else if (position == 1) {
            fragment = new FragmentTwo();
        } else if (position == 2) {
            fragment = new FragmentThree();
        }
        return fragment;
    }

Or maybe I can pass value to another fragment with the same method and I do not know how. I just started to learn android. Trying to understand how it works :/

Szarik
  • 23
  • 1
  • 3
  • There's also [android.stackexchange](https://android.stackexchange.com/) – woodvi Jan 03 '18 at 23:56
  • @woodvi What? Why did you write it? – Szarik Jan 03 '18 at 23:59
  • The log is saying that this line "FragmentThree f = (FragmentThree) getSupportFragmentManager().findFragmentByTag(tag);" is returning null. The fragment manager cannot find your fragment, which probably means it hasn't been attached yet. – andrewoid Jan 04 '18 at 00:25
  • @andrewoid So where or how should I do that? – Szarik Jan 04 '18 at 00:29
  • The FragmentPagerAdapter is responsible for this. Updating the contents of your adapter's fragments is somewhat tricky and there are several ways to do it. Does this crash happen if you scroll to the other fragments before pressing this button? If so, then I don't understand what's happening because all of the fragments should be attached by then I would think – andrewoid Jan 04 '18 at 01:19
  • @andrewoid it's happening after I click on the button. I can switch between fragments and everything works fine – Szarik Jan 04 '18 at 08:45
  • @andrewoid how I can attach this fragment? – Szarik Jan 04 '18 at 09:23

2 Answers2

0
FragmentThree f = (FragmentThree) getSupportFragmentManager().findFragmentByTag(tag);

This is returning Null because FragmentThree is not inflating in Activity.When we are using ViewPager then only the previous and next Fragment is inflate Not the All Fragment.

Shubhamhackz
  • 5,977
  • 5
  • 42
  • 65
Saurabh Vadhva
  • 624
  • 5
  • 12
0

User this code to resolve this problem

In Activity @Override public void sendDataLatLon(String message) { String tag = "android:switcher:" + R.id.viewPager + ":" + 2; viewPagerAdapter.sendData(message); }

In ViewPagerAdapter Class String message = "";

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    if (position == 0) {
        fragment = new FragmentOne();
    } else if (position == 1) {
        fragment = new FragmentTwo();
    } else if (position == 2) {
        fragment = new FragmentThree(message);
    }
    return fragment;
}

@Override
public int getCount() {
    return 3;
}

public void sendData(String message) {
    this.message = message;
}

In FragmentThree Fragment String message1 = "";

public FragmentThree(String message1) {
    this.message1 = message1;
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_fragment_three, container, false);
    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    fragThreeTv = (TextView) view.findViewById(R.id.fragThreeTv);
    fragThreeTv.setText(message1);

}
Saurabh Vadhva
  • 624
  • 5
  • 12
  • could you format it, cause I don't get it all... I can't do : fragment - new FragmentThree(message) - in viewPageAdapter – Szarik Jan 04 '18 at 08:51
  • please create the FragmentThree constructor and pass the string file. Its very simple – Saurabh Vadhva Jan 04 '18 at 09:43
  • Where should I create it? In FragmentOne - from where I'm sending String or in FragmentThree where I'm receiving? – Szarik Jan 04 '18 at 09:50
  • I think you are not checking my code.Please pass the message string into adapter class after then you can send the message string into FragmentThree class.like this..fragment = new FragmentThree(message); – Saurabh Vadhva Jan 04 '18 at 10:03