我正在從主要活動開始一項新活動。當單擊主活動上的單選按鈕時,我需要顯示時間選取器對話框。當我點擊單選按鈕時出現錯誤,該對話框將顯示給我。使用時間選擇器的自定義對話框
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
只是爲了重申LogCat中的內容:「你是否在你的AndroidManifest.xml中聲明瞭這個活動? – Tim
@Tim請看看我的新Logcat – chitranna
我認爲你在'Activity'和'Fragment'之間有一些混淆。 'Tilldate'是一個片段,而不是一個活動。不幸的是,我對片段不夠精通,無法提供幫助,但似乎你不能聲稱你的片段是一個活動。 – Tim