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 :/