我想使用timePickerDialog开发简单的警报应用程序。问题是在extends fragment上未定义构造函数TimePickerDialog。

我该怎么办?有什么建议么 ?

请帮我。谢谢你的关心。

这是代码的一部分。

   public class ReminderFragment extends Fragment {

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_reminder, container, false);

    textAlarmPrompt = (TextView) v.findViewById(R.id.alarmprompt);

    buttonstartSetDialog = (Button) v.findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            textAlarmPrompt.setText("");
            openTimePickerDialog(false);

        }});
     return v;
   }

   private void openTimePickerDialog(boolean is24r){
    Calendar calendar = Calendar.getInstance();

    timePickerDialog = new TimePickerDialog(
            ReminderFragment.this,
            onTimeSetListener,
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE),
            is24r);
    timePickerDialog.setTitle("Set Alarm Time");

    timePickerDialog.show();

        }

     OnTimeSetListener onTimeSetListener= new OnTimeSetListener(){

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        Calendar calNow = Calendar.getInstance();
        Calendar calSet = (Calendar) calNow.clone();

        calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calSet.set(Calendar.MINUTE, minute);
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);

        if(calSet.compareTo(calNow) <= 0){
            //Today Set time passed, count to tomorrow
            calSet.add(Calendar.DATE, 1);
        }

        setAlarm(calSet);
    }};
   }

最佳答案

使用getActivity()代替此ReminderFragment.this。第一个参数是上下文ReminderFragment.this指的是ReminderFragmentgetActivity()返回此片段与之关联的活动。

 timePickerDialog = new TimePickerDialog(
        getActivity(),
        onTimeSetListener,
        calendar.get(Calendar.HOUR_OF_DAY),
        calendar.get(Calendar.MINUTE),
        is24r);

关于android - 构造函数TimePickerDialog未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23156642/

10-11 00:09