user1334616
user1334616

Reputation:

Unable to call method from class instance Android

I've created a class that updates a class variable. I'm creating a new instance of the class then trying to call a get method. But i'm getting a 'canno't resolve method error and I have no idea why.

public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

    @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);
    }
    int selectedDate;
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        selectedDate = day+month+year;
    }
    public int getSelectedDate() {
        return selectedDate;
    }

}
public void showDatePickerDialog(View view) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
    int output = newFragment.getSelectedDate();
}

Upvotes: 0

Views: 314

Answers (2)

Manuel Spigolon
Manuel Spigolon

Reputation: 12890

the error is here:

public void showDatePickerDialog(View view) {
   DialogFragment newFragment = new DatePickerFragment();
   newFragment.show(getFragmentManager(), "datePicker");
   int output = newFragment.getSelectedDate();
}

your variable newFragment is a DialogFragment. So you must cast the variable or change the declaration like this:

DatePickerFragment newFragment = new DatePickerFragment();

or

int output = ((DatePickerFragment)newFragment).getSelectedDate();

EDIT: for get the selected date you must use Listener and not a sync method. so you have to change in this way your code:

public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private MyListener listener;
public DatePickerFragment(MyListener listener){
   this.listener = listener;
}

@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);
}
int selectedDate;
public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    selectedDate = day+month+year;
    if(listener!=null){
        listener.theDateHasChanged(selectedDate);
    }
}
public int getSelectedDate() {
    return selectedDate;
}

And then your activity must implement the MyListener interface and implement theDateHasChanged and in the end you must instantiate your fragment from your activity so:

DatePickerFragment newFragment = new DatePickerFragment(this);

Upvotes: 2

Aradhna
Aradhna

Reputation: 983

Maybe that's because your class is static. In java, you cannot create an instance of a class that is defined as static. For a static class, you can directly call the methods by using .();

DatePickerFragment.show(getFragmentManager(),"datePicker");

Hope this helps. :)

Upvotes: -1

Related Questions