我有一个片段,每个片段包含一个控件和一个textview,它们叠加在一起。这很好用。什么是无效的是这些片段中的每一个都是使用newInstance方法创建的,这样我就可以将数据传递给它(questionID和问题文本)。问题文本意味着显示在控件上方(无论是微调器,无线电组,还是其他),但它总是空白,如果我尝试直接调用该参数,我会一直得到空指针错误。
片段代码
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();
答案 0 :(得分:0)
在我看来,这一行造成了问题:
firstFragment.setArguments(getIntent().getExtras());
注释并检查问题是否仍然存在。可能您的Bundle
不包含密钥:ARG_PARAM1,ARG_PARAM2。