1

I have this piece of code that 2 EditText are using. The first one is work well but the second one is causing the application to crash with the error below. Any advise? I have taken a look at this question but it doesn't have a working solution.

public class EditTextDatePicker extends AppCompatEditText {
    public EditTextDatePicker(Context context) {
        super(context);
        this.initialize();
    }
    public EditTextDatePicker(Context context, AttributeSet attributeSet){
        super(context, attributeSet);
        this.initialize();
    }
    public EditTextDatePicker(Context context, AttributeSet attributeSet, int defStyle){
        super(context, attributeSet, defStyle);
        this.initialize();
    }

    private void initialize(){
        this.setTextIsSelectable(true);
        this.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    DatePicker dialog = new DatePicker(v);
                    dialog.show(TaskCreateUpdateDelete.fragmentTransaction, "DatePicker");
                }
            }
        });
    }
}

Here is the error:

05-22 23:26:31.479 1703-1703/comp3350.taskmanager E/InputEventReceiver: Exception dispatching input event.
05-22 23:26:31.480 1703-1703/comp3350.taskmanager E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
05-22 23:26:31.481 1703-1703/comp3350.taskmanager E/MessageQueue-JNI: java.lang.IllegalStateException: commit already called
                                                                      at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:625)
                                                                      at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
                                                                      at android.support.v4.app.DialogFragment.show(DialogFragment.java:156)
                                                                      at views.EditTextDatePicker$1.onFocusChange(EditTextDatePicker.java:36)
                                                                      at android.view.View.onFocusChanged(View.java:5206)
Community
  • 1
  • 1
Bob Ezuba
  • 510
  • 1
  • 5
  • 22

1 Answers1

0

Found the cause as explained in another post...

java.lang.IllegalStateException: commit already called

shows that the FragmentTransaction has been completed after calling commit() the first time and you are again calling commit() which tends to complete it once again. Hence it makes an Illegal state for the FragmentTransaction.

I changed my DialogFragment to look like this

public class EditTextDatePicker extends AppCompatEditText{
    FragmentManager fragmentManager = null;
    ....
    private void initialize(){
        this.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                DatePicker dialog = new DatePicker(v);
                dialog.show(fragmentManager.beginTransaction(), "DatePicker");
            }
        }
    });
}

And then I added this to my fragment class

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.something, container, false);
    EditTextDatePicker date_picker = (EditTextDatePicker) view.findViewByid(R.id.my_custom_edit_text);
    date_picker.fragmentManager = getActivity().getSupportFragmentManager();
    return view;
}
Bob Ezuba
  • 510
  • 1
  • 5
  • 22