Android Fragment Transaction - 已经调用的提交

时间:2016-05-23 04:56:17

标签: android android-edittext

我有2个EditText正在使用的这段代码。第一个是运行良好,但第二个是导致应用程序崩溃与下面的错误。有什么建议吗?我看过this question,但它没有可行的解决方案。

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

这是错误:

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)

1 个答案:

答案 0 :(得分:0)

找到原因,如另一篇文章中所解释的那样......

java.lang.IllegalStateException: commit already called

显示FragmentTransaction在第一次调用commit()之后已经完成,并且您再次调用commit(),这往往会再次完成它。因此,它会使FragmentTransaction成为非法状态。

我将DialogFragment改为看起来像这样

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

然后我将其添加到我的片段类

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