4

I have a simple Android Fragment CreateAccountBookingFragment which inherits from CreateAccountFragment, that has the following method :

@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    passwordTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    || (actionId == EditorInfo.IME_ACTION_DONE)) {
                onSignUpClick();
            }
            return false;
        }
    });
}

method onSignUpClick() is overridden in CreateAccountBookingFragment. Recently there was a bug caught :

java.lang.NullPointerException
   at com.myapp.ui.auth.CreateAccountBookingFragment.onSignUpClick(CreateAccountBookingFragment.java:82)
   at com.myapp.ui.auth.CreateAccountFragment$1.onEditorAction(CreateAccountFragment.java:83)
   at android.widget.TextView.onEditorAction(TextView.java:4594)
   at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
   at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:297)
   at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:175)
   at android.app.ActivityThread.main(ActivityThread.java:5279)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
   at dalvik.system.NativeStart.main(NativeStart.java)

I quite don't understand how it could throw NullPointer. It means that inner class is still alive and outer is not ? I though it's impossible. Can anyone help me understand what's going on here ? And what will be the solution to this. Any help appreciated. Thanks.

Device the bug occurred : Samsung GT P3113

Update : method onSingUpClick() is here :

@SuppressWarnings("unused")
@OnClick(R.id.frag_auth_sign_up_btn) // line 82, this is ButterKnife annotation
protected void onSignUpClick() {

    String email = emailText.getText().toString().trim();
    String password = passwordText.getText().toString().trim();

    int errorMsg = 0;
    if (TextUtils.isEmpty(email)) {
        errorMsg = R.string.enter_email;
    } else if (!Utils.isValidEmail(email)) {
        errorMsg = R.string.invaild_email;
    } else if (TextUtils.isEmpty(password)) {
        errorMsg = R.string.enter_password;
    } else if (!Utils.isValidPassword(password)) {
        errorMsg = R.string.invaild_password;
    } else if (!NetworkUtils.isNetworkConnected(getActivity())) {
        errorMsg = R.string.network_not_connected;
    }

    if (errorMsg > 0) {
        ViewUtils.getInfoDialog(getActivity(), errorMsg).show();
    } else {
        showLoadingDialog();
        signUp(email, password);
    }
    ViewUtils.hideKeyboard(getActivity());
}
Andrii Bas
  • 613
  • 1
  • 6
  • 20
  • 1
    If a non-static inner class is Serializable, it serializes the outer class too, and might indeed produce or reloading an OuterClass.this being null when the OuterClass is not Serializable. _Though that is quite a special case._ – Joop Eggen Aug 12 '15 at 14:08
  • 7
    post the contents of `onSignUpClick` and tell us what's line no 82) – Vinay W Aug 12 '15 at 14:09
  • Notice how it doesn't say onEditorAction is the problem it says onSignUpClick is the problem, specifically like 82. – JoxTraex Aug 13 '15 at 00:54
  • @VinayWadhwa updated with the function `onSignUpClick()`. But as far as I understood, the exception says the problem is in calling the function, not inside the function. – Andrii Bas Aug 13 '15 at 11:47

3 Answers3

2

Can you post the code of your signUp() function from the CreateAccountBookingFragment class? Are you in some way using getActivity() in that method?

There is chance that sometimes getActivity() returns null. It is most probably that error. So in this case, add a null check on getActivity(), that is:

if (getActivity()!=null){
    // only then do your singIn() method stuff 
}
Leigh
  • 28,765
  • 10
  • 55
  • 103
Aman Satija
  • 1,865
  • 2
  • 15
  • 16
1

I have a similar problem. I'm curious if my 'solution' will work for you as well. This problem is also occurring on a Samsung GT P3113 for me.

I moved the anonymous listener to the class itself eliminate the inner classs accessor. for you that would mean adding TextView.OnEditorActionListener to the 'implements' part of your Fragment class definition and then implementing the onEditorAction() method in the fragment class. That way the lifetime of the listener object is always the same as that of the fragment. I know, it shouldn't be a problem, but since the NPE is occurring when it tries to invoke the method using the accessor object.. it seems to be working for me.

dangVarmit
  • 5,641
  • 2
  • 22
  • 24
0

Maybe something you are using in CreateAccountBookingFragment class, any variable used from outside the anonymous inner class has to be final or it will have an issue due to cloistering.

Doug Ray
  • 988
  • 2
  • 9
  • 29
  • what about the fragment view itself :) ? if its being accessed in code in his method onSignUpClick(); – Doug Ray Aug 13 '15 at 00:49