I'm working on a tab layout with several fragments which need to have an object passed to them from a previous activity so they can populate their lists, and I'm running into an error when I try to retrieve that object from the bundle. I have previously passed this same object between other activities with no issue. I made the object Parcelable so I can add it to a bundle and pass it as an argument. Here is the code for the adapter class that spits out the Fragments used in the tab layout:
package edu.uml.android.adventurersarchive;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import edu.uml.android.adventurersarchive.character.CharacterInfo;
public class SpellbookTabAdapter extends FragmentPagerAdapter {
private Context mContext;
private CharacterInfo myCharacter;
public SpellbookTabAdapter(Context context, CharacterInfo ch, FragmentManager fm) {
super(fm);
mContext = context;
myCharacter = ch;
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
if(position == 0) {
PreparedSpellsFragment prepared = new PreparedSpellsFragment();
bundle.putParcelable("character", myCharacter);
prepared.setArguments(bundle);
return prepared;
} else if(position == 1) {
MySpellbookFragment myspellbook = new MySpellbookFragment();
bundle.putParcelable("character", myCharacter);
myspellbook.setArguments(bundle);
return myspellbook;
} else if(position == 2) return new FullSpellbookFragment();
else return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0) return "Prepared";
else if(position == 1) return "My Spells";
else if(position == 2) return "All Spells";
else return "";
}
}
Here is the PreparedSpellsFragment class. The code is almost identical in the MySpellbookFragment class (with the exception of how the fragment interacts with the object):
package edu.uml.android.adventurersarchive;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import java.util.List;
import java.util.Map;
import edu.uml.android.adventurersarchive.character.CharacterInfo;
import edu.uml.android.adventurersarchive.info.Spell;
public class PreparedSpellsFragment extends Fragment {
private SpellListAdapter adapter;
private List<String> groupHeaders;
private Map<String, List<Spell>> groupItems;
public PreparedSpellsFragment() {
Bundle bundle = getArguments();
CharacterInfo myCharacter = (CharacterInfo) bundle.getParcelable("character");
prepareGroups(myCharacter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.spell_list, container, false);
adapter = new SpellListAdapter(groupHeaders, groupItems);
ExpandableListView expView = (ExpandableListView) rootView.findViewById(R.id.spell_list);
expView.setAdapter(adapter);
expView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
// TODO: Launch activity to show detailed information about the spell.
return false;
}
});
return rootView;
}
private void prepareGroups(CharacterInfo myCharacter) {
// TODO: Take spells in player's prepared list and populate the groups and their items.
if(myCharacter != null) {
}
}
}
I get an error saying that in the PreparedSpellsFragment constructor, I'm invoking the "getParcelable()" method on a null object reference, suggesting the Bundle object retrieved from "getArguments()" is null, but I can't see any reason for the bundle itself to be null, short of maybe an OutOfMemoryException, where it could not allocate space for the object. I could understand if maybe the object just hadn't been passed correctly or if there was a typo in the string for the key representing the object, but no... it says the Bundle object itself is null.
Can anyone figure out what the cause is?