带有Timepicker的自定义对话框

时间:2012-10-16 19:48:20

标签: android android-widget

我正在从主要活动开始一项新活动。当点击主活动上的单选按钮时,我需要显示一个Time Pickerdialog。当我点击单选按钮时出现错误,我想给我看对话框。

CODE

import java.util.Calendar; 
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class Tilldate extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.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
    }

}

logcat的

10-16 13:01:37.539: E/AndroidRuntime(1435): FATAL EXCEPTION: main
10-16 13:01:37.539: E/AndroidRuntime(1435): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.avst.callxpressmobile/com.example.avst.callxpressmobile.Tilldate}: java.lang.ClassCastException: com.example.avst.callxpressmobile.Tilldate cannot be cast to android.app.Activity
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.os.Looper.loop(Looper.java:137)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at java.lang.reflect.Method.invoke(Method.java:511)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at dalvik.system.NativeStart.main(Native Method)
10-16 13:01:37.539: E/AndroidRuntime(1435): Caused by: java.lang.ClassCastException: com.example.avst.callxpressmobile.Tilldate cannot be cast to android.app.Activity
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-16 13:01:37.539: E/AndroidRuntime(1435):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-16 13:01:37.539: E/AndroidRuntime(1435):     ... 11 more

对于我的主要活动,请查看

Number of Custom Dialogs (Date and Time Picker) with relation to Radio Buttons

1 个答案:

答案 0 :(得分:0)

由于它不是活动,因此从AndroidManifest.xml中删除Tilldate,然后假设您正在调用startActivity, 使用

  DialogFragment dialog = new Tilldate();
  

对话框.show(getFragmentManager(),“对话框”);

  dialog .show(getFragmentManager().beginTransaction(), "dialog");

而不是startActivity

如果需要自定义对话框,请扩展TimePickerDialog。