2

我正在从主要活动开始一项新活动。当单击主要活动上的单选按钮时,我需要显示时间选择器对话框。当我单击应该向我显示对话框的单选按钮时出现错误。

代码

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

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

与单选按钮相关的自定义对话框(日期和时间选择器)的数量

4

1 回答 1

0

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

  DialogFragment dialog = new Tilldate();

对话框 .show(getFragmentManager(), "对话框");

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

而不是 startActivity

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

于 2012-10-16T20:59:47.023 回答