【问题标题】:Custom dialog with a Timepicker带有时间选择器的自定义对话框
【发布时间】:2012-10-16 19:48:20
【问题描述】:

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

代码

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

【问题讨论】:

  • 只是重申一下 LogCat 中的内容:have you declared this activity in your AndroidManifest.xml? ?
  • @Tim 请看看我的新Logcat
  • 我认为您在Activity 和Fragment 之间存在一些混淆。 Tilldate 是一个片段,而不是一个活动。不幸的是,我对片段不够精通,无法提供帮助,但您似乎不能声称您的片段是一个 Activity。
  • @Tim,我该如何解决?我以前的活动需要它。有什么建议、想法吗??

标签: android android-widget


【解决方案1】:

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

  DialogFragment dialog = new Tilldate();

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

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

而不是 startActivity

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

【讨论】:

  • 在示例中,他们正在调用对话框,而您已经创建了一个 dialogFragment。如果您想要自定义 timepickerdialog 使用 TimePickerDialog 扩展 Tilldate 而不是 dialogfragment
  • show 出现错误 - The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (FragmentManager, String)
  • 当我更改单选按钮时,我只需要一个自定义对话框。你能帮我怎么做吗? (stackoverflow.com/questions/12908902/…)
  • 片段对我来说可能太复杂了。有什么办法可以实现吗??
猜你喜欢
  • 2015-05-28
  • 2012-07-09
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多