1

我想從我的主Activity使用DialogFragments創建一個DatePicker對話框。我已經擴展了DialogFragment類,並返回新datePickerDialog按java.lang.IllegalStateException:在創建日期選擇器對話框時活動已被破壞

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

} 

我對主Acivity一個按鈕來觸發對話框:

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 has been destroyed」異常在bel流線:

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) 

任何幫助將非常感激。

謝謝。

回答

0

今天面臨着類似的問題。這很簡單。只是檢查我的按鈕點擊以下:

if (!datePickerDialog.isAdded()) { 

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

就是這樣

+0

嗨,我試過這個解決方案,但我仍然得到相同的錯誤 – flamesavor

相關問題