【问题标题】:Arguments bundle not getting populated in my fragment Android我的片段 Android 中没有填充参数包
【发布时间】:2015-03-01 10:32:13
【问题描述】:

我有一个片段,每个片段都包含一个控件和一个文本视图,它们相互堆叠。这工作正常。不起作用的是这些片段中的每一个都是使用 newInstance 方法创建的,以便我可以将数据传递给它(questionID 和 question Text)。问题文本旨在显示在控件上方(无论是微调器、无线电组等),但它始终为空白,如果我尝试直接调用该参数,我会不断收到空指针错误。

片段代码

public class RadioFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mQuestionID;
private String mQuestionText;

private OnFragmentInteractionListener mListener;
private Button mSaveButton;
private TextView mQuestion;

/**
 * @param questionID Parameter 1.
 * @param questionText Parameter 2.
 * @return A new instance of fragment RadioFragment.
 */
// TODO: Rename and change types and number of parameters
public static RadioFragment newInstance(String questionID, String questionText) {
    RadioFragment fragment = new RadioFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, questionID);
    args.putString(ARG_PARAM2, questionText);
    fragment.setArguments(args);
    return fragment;
}

public RadioFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mQuestionID = getArguments().getString(ARG_PARAM1);
        mQuestionText = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_radio, container, false);

    //set the question text

    mQuestion = (TextView) rootView.findViewById(R.id.questionText);
    mQuestion.setText(mQuestionText);

    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public interface OnFragmentInteractionListener {
    //public void onButton(String questionType, String question);
}
}

MainActivity 中片段被实例化的部分:

String questionID = "someID";
 String questionText = "This is a sample question";
// Create a new Fragment to be placed in the activity layout
            RadioFragment firstFragment = RadioFragment.newInstance(questionID, questionText);
            //RadioFragment firstFragment = new RadioFragment(questionID, questionText);

            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getFragmentManager().beginTransaction().add(R.id.container, firstFragment, "fragment" + i).commit();

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    在我看来,这条线引起了问题:

    firstFragment.setArguments(getIntent().getExtras());
    

    将其注释掉并检查问题是否仍然存在。可能您的Bundle 不包含键:ARG_PARAM1、ARG_PARAM2。

    【讨论】:

    • 是的,这绝对是它!谢谢你。这是我创建活动时从 IDE 生成的默认代码,它基本上用 Intent 附加功能(其中没有)覆盖了我的参数。谢谢!
    • @Bec Martin stackoverflow.com/questions/67042935/… 告诉我兄弟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多