我正在制作一个带有多个片段的选项卡布局,这些片段需要将一个对象从之前的活动传递给它们,以便它们可以填充它们的列表,并且当我试图从束。我之前已经通过其他活动之间没有问题的同一个对象。我创建了对象Parcelable,这样我就可以将它添加到一个包中,并将它作为参数传递。下面是该吐出的标签布局中使用的片段适配器类代码:传递给Fragment的包是否为空?
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 "";
}
}
这里是PreparedSpellsFragment类。该代码是在MySpellbookFragment类几乎相同(使用的片段是如何与对象进行交互除外):
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) {
}
}
}
我得到一个错误,指出在PreparedSpellsFragment构造函数,我调用“getParcelable() “方法在一个空的对象引用,暗示从”getArguments()“检索到的Bundle对象为空,但我看不到任何理由,捆绑本身为null,可能是一个OutOfMemoryException,它不能分配空间为对象。我可以理解,如果对象可能没有被正确传递,或者如果在表示对象的键的字符串中存在拼写错误,但是没有......它表示Bundle对象本身为空。
任何人都可以找出原因是什么?
碎片不应该有构造函数 –
你可能会觉得这篇文章有用。 http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment –