从片段加载timePickerDialog

时间:2015-10-25 14:47:48

标签: android datetimepicker timepicker layout-inflater findviewbyid

我正在尝试使用自定义时间选择器,然后从片段中运行选择器。这是时间选择器对话框的代码(TimePickerFragment.java):

package com.ravit.android.aroundtheclock;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TimePicker;

import java.util.Calendar;

/**
 * A simple {@link Fragment} subclass.
 */
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {


    public TimePickerFragment() {
        // Required empty public constructor
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker

        final Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_time_picker, container, false);
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}

这是来自onCreateView片段的代码,它显示了时间选择器(WorkdayFragment.java):

mButtonEnd = (Button)v.findViewById(R.id.end_workday);
mButtonEnd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        TimePickerFragment dialog = new TimePickerFragment();
        dialog.show(getActivity().getFragmentManager(), DIALOG_TIME);
    }
});

这是按下按钮时出现的错误。加载视图似乎有问题:

10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: FATAL EXCEPTION: main
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: Process: com.ravit.android.aroundtheclock, PID: 20325
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:317)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at com.android.internal.app.AlertController.installContent(AlertController.java:231)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.AlertDialog.onCreate(AlertDialog.java:423)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.Dialog.show(Dialog.java:295)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.DialogFragment.onStart(DialogFragment.java:499)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.Fragment.performStart(Fragment.java:2244)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.BackStackRecord.run(BackStackRecord.java:793)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-25 16:24:46.696 20325-20325/com.ravit.android.aroundtheclock E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的猜测是时间选择器代码的编写方式存在问题,因为当我用日期选择器替换它时,一切都运行没有错误。

我尝试根据关于此主题的其他答案解决requestFeature()错误,但没有任何进展。

关于如何从这里继续的任何建议都会非常有帮助。 谢谢!

2 个答案:

答案 0 :(得分:2)

首先,您需要删除timepicker片段的onCreateView。 我也可以在例子中找到与我分享。

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

所以你的TimerPickerFragment应该如下所示

package com.ravit.android.aroundtheclock;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TimePicker;

import java.util.Calendar;

/**
 * A simple {@link Fragment} subclass.
 */
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {


    public TimePickerFragment() {
        // Required empty public constructor
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker

        final Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
    }
}
  

警告:如果您的应用支持的Android版本低于3.0,请执行   确保调用getSupportFragmentManager()来获取实例   FragmentManager。还要确保显示您的活动   时间选择器扩展了FragmentActivity而不是标准   活动类。

mButtonEnd = (Button)v.findViewById(R.id.end_workday);
mButtonEnd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        DialogFragment newFragment  = new TimePickerFragment();
        //newFragment.show(getActivity().getFragmentManager(), DIALOG_TIME);
        newFragment.show(getSupportFragmentManager(), DIALOG_TIME);
        // if you are using the nested fragment then user the 
        //newFragment.show(getChildFragmentManager(), DIALOG_TIME);
    }
});

如果您有任何疑问,请与我们联系。感谢

答案 1 :(得分:1)

这是一个完整的例子,可以从Fragment和Activity中使用:

public class TimePickerDialogFragment extends DialogFragment {

    private static final String ARGUMENT_HOUR = "ARGUMENT_HOUR";
    private static final String ARGUMENT_MINUTE = "ARGUMENT_MINUTE";
    private static final String ARGUMENT_IS_24_HOURS = "ARGUMENT_IS_24_HOURS";
    private TimePickerDialog.OnTimeSetListener listener;

    private int hourOfDay;
    private int minute;
    private boolean is24Hours;

    public static TimePickerDialogFragment newInstance(final int hour, final int minute, final boolean is24Hours) {
        final TimePickerDialogFragment df = new TimePickerDialogFragment();
        final Bundle args = new Bundle();
        args.putInt(ARGUMENT_HOUR, hour);
        args.putInt(ARGUMENT_MINUTE, minute);
        args.putBoolean(ARGUMENT_IS_24_HOURS, is24Hours);
        df.setArguments(args);
        return df;
    }

    @Override
    public void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        retrieveArguments();
    }

    private void retrieveArguments() {
        final Bundle args = getArguments();
        if (args != null) {
            hourOfDay = args.getInt(ARGUMENT_HOUR);
            minute = args.getInt(ARGUMENT_MINUTE);
            is24Hours = args.getBoolean(ARGUMENT_IS_24_HOURS);
        }
    }

    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        return new TimePickerDialog(getContext(), this.listener, this.hourOfDay, this.minute, this.is24Hours);
    }

    public void setListener(final TimePickerDialog.OnTimeSetListener listener) {
        this.listener = listener;
    }
}

初始化并显示:

final Calendar c = Calendar.getInstance();
final boolean is24Hours = DateFormat.is24HourFormat(getContext());
final TimePickerDialogFragment timePicker = TimePickerDialogFragment.newInstance(
        c.get(Calendar.HOUR_OF_DAY),
        c.get(Calendar.MINUTE),
        is24Hours);
timePicker.setListener(this);
timePicker.showNow(getChildFragmentManager(), null);