简体   繁体   中英

java.lang.IllegalStateException: Activity has been destroyed on creating a datepicker dialog

I am trying to create a DatePicker dialog from my main Activity using DialogFragments . I have extended the DialogFragment class and return the new datePickerDialog as per

http://developer.android.com/guide/topics/ui/controls/pickers.html

Here is my class code:

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);
    }

}

I have a button on the main Acivity to fire the dialog:

public void setAlarm(View view){
        //TextView reminder_title  = (TextView) view.findViewById(R.id.reminder_title); 
        DialogFragment dateFragment = new DatePickerFragment();
        dateFragment.show(getFragmentManager(), "date picker");
    }

However, when I try to click on the button, I get an "java.lang.IllegalStateException: Activity has been destroyed" exception at the below line:

dateFragment.show(getFragmentManager(), "date picker");

Here is the activity log:

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)

Any help on this will be really appreciated.

Thanks.

Look at this example maybe it helps you find the right solution:

How to make a DatePicker with DialogFragment and FragmentManage

Faced similar issue today. It was simple. Just check the following on my button click:

if (!datePickerDialog.isAdded()) {

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

that's it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM