繁体   English   中英

捆绑传递给Fragment是否为null?

[英]Bundle being passed to Fragment is null?

我正在使用带有几个片段的选项卡布局,这些片段需要从上一个活动传递给他们一个对象,以便他们可以填充其列表,并且当我尝试从包中检索该对象时遇到了错误。 我以前在其他活动之间传递了相同的对象,没有任何问题。 我将对象设置为Parcelable,因此可以将其添加到包中并将其作为参数传递。 以下是适配器类的代码,这些代码吐出了选项卡布局中使用的“片段”:

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,在OutOfMemoryException中无法为对象分配空间。 我可以理解该对象是否只是未正确传递,或者表示该对象的键的字符串中是否有错字,但是不……它表示Bundle对象本身为null。

谁能找出原因?

Bundle bundle = getArguments();
CharacterInfo myCharacter = (CharacterInfo) bundle.getParcelable("character");
prepareGroups(myCharacter);

将此代码写入onCreateView()并检查。

您正在检查构造函数中的Bundle。 摆脱构造函数,然后检查片段的onCreateView()中的Bundle。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM