我正在尝试将一些数据从datepickerfragment传递到调用where的片段但是在尝试获取数据时我得到nullpointer,这是我的datepicker代码
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
CreateFragment createFragment = new CreateFragment();
Bundle arguments = new Bundle();
arguments.putInt("year",year);
arguments.putInt("month",month);
arguments.putInt("day",dayOfMonth);
createFragment.setArguments(arguments);
month = month+1;
textviewDate = (TextView)getActivity().findViewById(R.id.textviewDate);
textviewDate.setText(dayOfMonth+"/"+month+"/"+year);
}
以下是我尝试获取数据的代码:
public boolean checkDate(){
Bundle bundle = getArguments();
pickedYear = bundle.getInt("year");
pickedMonth = bundle.getInt("month");
pickedDay = bundle.getInt("day");
}
但我总是得到nullpointer,这是我得到的错误: java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int android.os.Bundle.getInt(java.lang.String)'
顺便说一句,datepickerfragment是从同一个片段CreateFragment调用的。 希望有人可以帮助我。
这是片段:
public class CreateFragment extends Fragment {
//Codigo que envio al intent para saber que es mi request
private static final int PICK_CONTACT_CODE = 1;
private Button btnSearchContact,btnDateChange,btnTimeChange;
private FloatingActionButton fab;
private EditText edittextPhoneNumber,edittextMessage;
private TextView textviewDate,textviewTime;
private int column,day,month,year,hour,minute,timeOfDay,pickedYear,pickedMonth,pickedDay;
private Calendar calendar;
private String contactPhoneNumber,currentDate,currentTime,morningOrAfternoon;
private View rootView;
private Realm realm;
public CreateFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_create, container, false);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Haciendo referencia a los componentes
edittextPhoneNumber = (EditText)rootView.findViewById(R.id.edittextPhoneNumber);
edittextMessage = (EditText)rootView.findViewById(R.id.edittextMessage);
btnSearchContact = (Button)rootView.findViewById(R.id.buttonContacts);
btnDateChange = (Button)rootView.findViewById(R.id.buttonDateChange);
btnTimeChange = (Button)rootView.findViewById(R.id.buttonTimeChange);
fab = (FloatingActionButton)rootView.findViewById(R.id.fab);
textviewDate = (TextView)rootView.findViewById(R.id.textviewDate);
textviewTime = (TextView)rootView.findViewById(R.id.textviewTime);
//Referencia a la base de datos
realm = Realm.getDefaultInstance();
//Llamando al metodo para poner la fecha en los textview
setCurrentDate();
textviewDate.setText(currentDate);
textviewTime.setText(currentTime);
//Llamando al fragment con el picker de fecha
btnDateChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.support.v4.app.DialogFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getFragmentManager(),"datePicker");
}
});
//Llamando al fragment con el picker de hora
btnTimeChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.support.v4.app.DialogFragment timePickerFragment = new TimePickerDialog();
timePickerFragment.show(getFragmentManager(),"timePicker");
}
});
//Llamando a la app de contactos para elegir un numero
btnSearchContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent contactIntent = new Intent(Intent.ACTION_PICK,Uri.parse("content://contacts"));
contactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(contactIntent,PICK_CONTACT_CODE);
}
});
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edittextPhoneNumber.getText().toString().isEmpty()){
Toast.makeText(getActivity(),R.string.toast_phone_empty,Toast.LENGTH_SHORT).show();
}else if (edittextMessage.getText().toString().isEmpty()){
Toast.makeText(getActivity(),R.string.toast_message_empty,Toast.LENGTH_SHORT).show();
}else if(checkDate()){
saveMessage();
Toast.makeText(getActivity(),R.string.toast_message_saved,Toast.LENGTH_SHORT).show();
edittextPhoneNumber.getText().clear();
edittextMessage.getText().clear();
//Haciendo que el teclado se esconda
InputMethodManager inputMethodManager = (InputMethodManager)getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
}
});
}
//Metodo para tomar la fecha actual y ponerla en los textview
public void setCurrentDate(){
//Haciendo las instancias del calendar y tomando la fecha y hora actual
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
//Le sumo 1 al mes porque inicia en 0
month = month+ 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
timeOfDay = calendar.get(Calendar.AM_PM);
if (timeOfDay <= 0){
morningOrAfternoon = "AM";
}else {
morningOrAfternoon = "PM";
}
currentDate = day+"/"+month+"/"+year;
//Calendar minute no agrega los ceros cuando es menos a 10 asi que hice un If
if (minute <=9){
currentTime = hour+":"+"0"+minute+" "+morningOrAfternoon;
}else {
currentTime = hour + ":" + minute + " " + morningOrAfternoon;
}
}
//Confirmando que la fecha no sea menor a la actual
public boolean checkDate(){
/*Bundle bundle = getArguments();
pickedYear = bundle.getInt("year");
pickedMonth = bundle.getInt("month");
pickedDay = bundle.getInt("day");
if (pickedYear < year){
Toast.makeText(getActivity(),R.string.toast_year_pass,Toast.LENGTH_SHORT).show();
return false;
}else if (pickedMonth < month){
Toast.makeText(getActivity(),R.string.toast_month_pass,Toast.LENGTH_SHORT).show();
return false;
}else if (pickedDay < day){
Toast.makeText(getActivity(),R.string.toast_day_pass,Toast.LENGTH_SHORT).show();
return false;
}else {
return true;
}*/
return true;
}
}
这是对话片段
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
final Calendar c = Calendar.getInstance();
private TextView textviewDate;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
/*CreateFragment createFragment = new CreateFragment();
Bundle arguments = new Bundle();
arguments.putInt("year",year);
arguments.putInt("month",month);
arguments.putInt("day",dayOfMonth);
createFragment.setArguments(arguments);*/
month = month+1;
textviewDate = (TextView)getActivity().findViewById(R.id.textviewDate);
textviewDate.setText(dayOfMonth+"/"+month+"/"+year);
}
}