Passing data between two Fragments in a VIewPager (Android) (NullPointerException)
android, android-fragments, android-viewpager, java, nullpointerexception
Solution
EDITED:
When you call `fragmentPagerAdapter.getItem(1)` you are getting a new instance of the fragment so you are referring to a different object. this is why the view is null and you get the NullPointerException. If you need an adapter for only 2 fragments, you can try with something like that:
public class YourPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {
private FragmentFavourites mFragFavourites;
private FragmentConverter mFragConverter;
public YourPagerAdapter() {
// ... your code above
this.mFragFavourites = new FragmentFavourites();
this.mFragConverter = new FragmentConverter();
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return mFragFavourites;
case 1:
return mFragConverter;
default:
return null;
}
}
}
Problem
So basically I have 2 Fragments - `FragmentConverter` and `FragmentFavourites`, and I have one `MainActivity`. I'm trying to pass 4 arrays from the first fragment to the second one using an Interface called `Communicator`. The specific snippets are show below: ``` public interface Communicator { public void respond(String[] names, String[] codes, String[] symbols, int[] images); } ``` This is a method inside `FragmentFavourites`: ``` @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub String[] checkedNames = new String[counter]; String[] checkedCodes = new String[counter]; String[] checkedSymbols = new String[counter]; int[] checkedImages = new int[counter]; comm = (Communicator) getActivity(); int index = 0; if (item.getItemId() == R.id.action_save){ for (int i=0;i<checked.size();i++){ if (checked.get(i) == true){ checkedNames[index] = names[i]; checkedCodes[index] = codes[i]; checkedSymbols[index] = symbols[i]; checkedImages[index] = images[i]; index++; } } comm.respond(checkedNames, checkedCodes, checkedSymbols, checkedImages); } return super.onOptionsItemSelected(item); } ``` This is the implemented interface method inside `MainActivity`: ``` @Override public void respond(String[] names, String[] codes, String[] symbols, int[] images) { // TODO Auto-generated method stub FragmentConverter frag = (FragmentConverter) fragmentPagerAdapter.getItem(1); frag.changeData(names, codes, symbols, images); } ``` And this is a method that collects the data in `FragmentConverter`: ``` public void changeData(String[] names, String[] codes, String[] symbols, int[] images){ this.names = names; this.codes = codes; this.symbols = symbols; this.images = images; Log.d("TEST", symbols.length + names.length + codes.length + images.length + ""); tvOneRate.setText(names[1]); } ``` Now the problem is that whenever I try to change a ui component inside `FragmentConverter`, I get a `NullPointerException`, though the Log.d statement returns the correct results. EDIT1: getItem() method of FragmentPagerAdapter: ``` @Override public Fragment getItem(int i) { // TODO Auto-generated method stub Fragment frag = null; if (i == 0){ frag = new FragmentFavourites(); } if (i == 1){ frag = new FragmentConverter(); } return frag; } ```