我正在尝试使用从我的主 Activity 创建一个 DatePicker 对话框DialogFragments
。我已经扩展了 DialogFragment 类并根据返回新的 datePickerDialog
http://developer.android.com/guide/topics/ui/controls/pickers.html
这是我的课程代码:
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;
public class DatePickerFragment extends DialogFragment implements OnDateSetListener{
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO set the date for the alarm
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//set current date as default date
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//Create New Instance of the datepicker dialog
return new DatePickerDialog(getActivity(), this, year, month, day);
}
}
我在主 Activity 上有一个按钮来触发对话框:
public void setAlarm(View view){
//TextView reminder_title = (TextView) view.findViewById(R.id.reminder_title);
DialogFragment dateFragment = new DatePickerFragment();
dateFragment.show(getFragmentManager(), "date picker");
}
但是,当我尝试单击该按钮时,在下面的行中出现“java.lang.IllegalStateException:Activity 已被破坏”异常:
dateFragment.show(getFragmentManager(), "date picker");
这是活动日志:
06-08 15:45:48.261: E/AndroidRuntime(5489): FATAL EXCEPTION: main
06-08 15:45:48.261: E/AndroidRuntime(5489): Process: com.flamesavor.reme, PID: 5489
06-08 15:45:48.261: E/AndroidRuntime(5489): java.lang.IllegalStateException: Activity has been destroyed
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1345)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.app.BackStackRecord.commit(BackStackRecord.java:575)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.app.DialogFragment.show(DialogFragment.java:230)
06-08 15:45:48.261: E/AndroidRuntime(5489): at com.flamesavor.reme.ReMe.setAlarm(ReMe.java:67)
06-08 15:45:48.261: E/AndroidRuntime(5489): at com.flamesavor.reme.resources.ListViewAdapter.setAlarm(ListViewAdapter.java:60)
06-08 15:45:48.261: E/AndroidRuntime(5489): at com.flamesavor.reme.resources.ListViewAdapter$1.onClick(ListViewAdapter.java:48)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.view.View.performClick(View.java:4456)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.view.View$PerformClick.run(View.java:18462)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.os.Handler.handleCallback(Handler.java:733)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.os.Handler.dispatchMessage(Handler.java:95)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.os.Looper.loop(Looper.java:136)
06-08 15:45:48.261: E/AndroidRuntime(5489): at android.app.ActivityThread.main(ActivityThread.java:5102)
06-08 15:45:48.261: E/AndroidRuntime(5489): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 15:45:48.261: E/AndroidRuntime(5489): at java.lang.reflect.Method.invoke(Method.java:515)
06-08 15:45:48.261: E/AndroidRuntime(5489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-08 15:45:48.261: E/AndroidRuntime(5489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-08 15:45:48.261: E/AndroidRuntime(5489): at dalvik.system.NativeStart.main(Native Method)
对此的任何帮助将不胜感激。
谢谢。