Reputation: 2136
The method setDateListener(DateListener dl)
cannot be resolved. It is public and I am using it on an object of the class DatePickerFragment.java where the method is contained.
Here is the onCreateView()
method in the fragment where the setDateListener()
method is called:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_sign_up_about, container, false);
mFirstNameEditText = (EditText) mView.findViewById(R.id.sign_up_first_name_edit_text);
mLastNameEditText = (EditText) mView.findViewById(R.id.sign_up_last_name_edit_text);
mBirthdayEditText = (EditText) mView.findViewById(R.id.sign_up_birthday_edit_text);
mContinueButton = (Button) mView.findViewById(R.id.sign_up_continue_2_button);
mBirthdayEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datePickerFragment = new DatePickerFragment();
datePickerFragment.setDateListener(SignUpAboutFragment.this);
datePickerFragment.show(getFragmentManager(), "datePicker");
}
});
}
Here is the DatePickerFragment.java class (imports have been omitted):
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private DateListener mCallback;
public interface DateListener {
void onDateSelected(String formattedDate);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = sdf.format(c.getTime());
mCallback.onDateSelected(formattedDate);
}
public void setDateListener(DateListener dateListener) {
mCallback = dateListener;
}
}
Upvotes: 0
Views: 5687
Reputation: 157457
The method setDateListener(DateListener dl) cannot be resolved. It is public and I am using it on an object of the class DatePickerFragment.java where the method is contained.
that happens when you assign the reference to the super type. DatePickerFragment
is a DialogFragment
, conversely a DialogFragment
is not a DatePickerFragment
Upvotes: 2