Pregunta

I was looking for in the forum and in the web, but I can't find a solution for my problem. I am working in a program that uses a DatePicker and a Timepicker. In my program I used the Google default definition for Pickers (http://developer.android.com/guide/topics/ui/controls/pickers.html) with a separeted class for each picker(time and date).

public void showDatePickerDialog(View v) {
    DatePickerFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");

}

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
}

This is the DatePickerFragment class:

    public class DatePickerFragment extends DialogFragment {

@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(),
            (OnDateSetListener) getActivity(), year, month, day);
}
    }

I read something about support libraries and to change my activity to FragmentActivity, but I got some errors. How should I change my code to implement that in versions lower than 3.0? Thank you in advance!

¿Fue útil?

Solución

Solution: The problem here was that I was using android.app.DialogFragment instead of android.support.v4 .app.Dialog.Fragment. So, I imported android.support.v4 library to my three classes (MainActivity, DatePickerFragment and TimePickerFragment.

Next step was changing the extends of my MainActivity class from Activity to FragmentActivity (changing the imports statements):

public class MainActivity extends FragmentActivity implements
        DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {}

Finally, I changed getFragmentManager() for getSupportFragmentManager() in both classes:

public void showDatePickerDialog(View v) {
    DatePickerFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker"); 
}

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}

Last step was testing on the android 2.3.3 emulator and it worked correctly.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top