Fragment TextWatcher 与 Activity 通信时出现问题

Trouble with Fragment TextWatcher to Activity communication

片段与 Activity 通信正常工作时出现问题..

我在 Fragment 中有一个编辑框,我需要它在每次编辑后将其值发送回主 activity。

片段Class:

public class FragA extends Fragment {

    FragAInt mCallback;

    public interface FragAInt{
        public void onEditBoxEdited(String boxContent);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view =  inflater.inflate(R.layout.fragment_a, container, false);
       final View et = view.findViewById(R.id.eboxa) ;

        ((EditText) et).addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                mCallback.onEditBoxEdited(((EditText) et).getText().toString());
            }
        });
        return view;
    }
}

主要Activity

public class MainActivity extends FragmentActivity implements FragA.FragAInt{

    private String editBoxVal;

    public void onEditBoxEdited(String editBoxContent){
        this.editBoxVal= editBoxContent;
    }
}

该应用程序将 运行 但只要您在退出框中键入空指针异常,它就会崩溃。

我哪里出错了?

完整错误:

java.lang.NullPointerException: 
Attempt to invoke interface method 'void nemo.myapplication.FragA$FragAInt.onEditBoxEdited(java.lang.String)' on a null object reference
at nemo.myapplication.FragA.afterTextChanged(FragA.java:49)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8017)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10182)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1043)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:560)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:492)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:34)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:685)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:445)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:340)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

您需要分配 mCallback 变量。我想你错过了那部分,你可以使用 onAttach 事件。

public class FragA extends Fragment {

    FragAInt mCallback;

    public interface FragAInt{
        public void onEditBoxEdited(String boxContent);
    }

    public FragA(FragAInt mCallback) {
        this.mCallback = mCallback;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (FragAInt) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view =  inflater.inflate(R.layout.fragment_a, container, false);
       final View et = view.findViewById(R.id.eboxa) ;

        ((EditText) et).addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                mCallback.onEditBoxEdited(((EditText) et).getText().toString());
            }
        });
        return view;
    }
}