显示datepicker和timepicker时出错

时间:2014-05-22 10:31:32

标签: android datepicker fragment timepicker

我遇到问题,我有2种方法来显示TimePicker和DatePicker。另外它们运作良好,但如果我需要将一个链接在另一个之后(实际上我想要一个带有时间和日期的选择器,但我还没有找到正确的方法)。所以,我发布了以下代码。

当我点击一个按钮时,我正在从片段中调用它,但我需要他们在我说的时候显示一个在其他后面。

public void showTimePickerDialog() {
        TimePickerFragment newFragment = new TimePickerFragment();
        newFragment.setTargetFragment(this, 0);
        newFragment.show(this.getFragmentManager(), "timePicker");
    }

public void showDatePickerDialog() {
        DatePickerFragment newFragment = new DatePickerFragment();
        newFragment.setTargetFragment(this, 0);
        newFragment.show(this.getFragmentManager(), "datePicker");
    }

public void onFinishTimePickerDialog(int hourOfDay, int minute) {
        tmp_hour = hourOfDay;
        tmp_minute = minute;

        String str_day, str_month, str_year, str_hour, str_minute;
        str_year = String.format("%d", tmp_year);
        str_month = String.format("%02d", tmp_month);
        str_day = String.format("%02d", tmp_day);
        str_hour = String.format("%02d", tmp_hour);
        str_minute = String.format("%02d", tmp_minute);

        ((CloseItActivity) getActivity()).setBuydate(str_day + "/" + str_month + "/" + str_year + " " + str_hour + ":" + str_minute);
    }
public void onFinishDatePickerDialog(int year, int month, int day) {
        tmp_year = year;
        tmp_month = month;
        tmp_day = day;
    }

谢谢。

编辑:如果我在showDatePickerDialog()中放置showTimePickerDialog(),应用程序崩溃,我认为这是一个上下文问题,但我不清楚。

记录类中的onDataSet有一个nullexception:

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.DatePicker;

import java.util.Calendar;

/**
 * Created by McFly on 10/05/2014.
 */
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    //Interface creada para implementar un método que podamos utilizar como
    //enlace de la actividad en curso y le enviemos la información
    //seleccionada por el usuario.

    public interface DatePickerFragmentListener{
        void onFinishDatePickerDialog(int year, int month, int day);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //Datos que seleccionara el usuario.
        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);

        // Creamos la instancia del DatePicker para devolverla.
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker arg0, int year, int month, int day) {
        // TODO Auto-generated method stub
        Log.i("LOG_PARENT_FRAG", "" + getParentFragment());
        DatePickerFragmentListener activity = (DatePickerFragmentListener) getParentFragment();
        activity.onFinishDatePickerDialog(year, month, day);

    }
}

getParentFragment为null,这就是问题所在。我如何将它传递给父片段的上下文?

0 个答案:

没有答案