我有一个简单的Android片段CreateAccountBookingFragment,它继承自CreateAccountFragment,它具有以下方法:

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

方法onSignUpClick()CreateAccountBookingFragment中被覆盖。
最近发现了一个错误:
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)

我完全不明白它怎么会抛出NullPointer。这意味着内部阶级还活着而外部不是吗?我虽然不可能。谁能帮我了解这里的事吗?对此的解决方案是什么。任何帮助表示赞赏。谢谢。

发生错误的设备:Samsung GT P3113

更新:
方法onSingUpClick()在这里:
@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());
}

最佳答案

您可以从CreateAccountBookingFragment类发布signUp()函数的代码吗?您是否以某种方式在该方法中使用getActivity()

有时候getActivity()有可能返回null。这很可能是该错误。因此,在这种情况下,请对getActivity()添加一个空检查,
那是:

if (getActivity()!=null){
    // only then do your singIn() method stuff
}

关于java - 匿名内部类中的NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31967469/

10-10 00:40