1

我正在尝试使用从我的主 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)

对此的任何帮助将不胜感激。

谢谢。

4

2 回答 2

0

今天遇到了类似的问题。这很简单。只需单击我的按钮检查以下内容:

if (!datePickerDialog.isAdded()) {

          datePickerDialog.setStartDate(year, month, day);
          datePickerDialog.show(getFragmentManager(), DATEPICKER_TAG);
}
else
{
  //you can replace or add,,ur wish
}

而已

于 2014-06-12T12:15:10.430 回答
0

看看这个例子也许它可以帮助你找到正确的解决方案:

如何使用 DialogFragment 和 FragmentManage 制作 DatePicker

于 2014-06-08T11:19:23.317 回答