Search code examples
androidandroid-fragmentsandroid-input-method

App crashes when navigating if input is focused


I have strange issue with an application I am building. I have a fragment with a login form, when fragment is opened first field is focused by default, then if I click on another field and afterwards navigate to other fragment I get following crash.

java.lang.NullPointerException: Attempt to invoke interface method 'void android.view.inputmethod.InputConnection.closeConnection()' on a null object reference
    at android.view.inputmethod.InputConnectionWrapper.closeConnection(InputConnectionWrapper.java:270)
    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:541)
    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Heres a login form layout, to point out that I am using databinding, although I am not sure if that can be related

<android.support.design.widget.TextInputEditText
    android:id="@+id/edit_login_form_email"
    style="@style/AppEditTextTheme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/login_form_edit_email"
    android:inputType="textEmailAddress"
    android:maxLines="1"
    android:text="@={loginModel.username}" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/input_login_form_edit_password"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true">

<android.support.design.widget.TextInputEditText
    style="@style/AppEditTextTheme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/login_form_edit_password"
    android:inputType="textPassword"
    android:maxLines="1"
    android:text="@={loginModel.password}" />
</android.support.design.widget.TextInputLayout>

<Button
    android:id="@+id/button_login_form_register"
    style="@style/Base.Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:onClick="@{() -> interaction.onRegisterClick()}"
    android:text="@string/login_form_button_register"
    android:textColor="@color/regularGrey" />

Navigation to other fragment:

override fun onRegisterClick() {
    fragmentManager!!.beginTransaction()
            .replace(R.id.fragment_container, RegisterFragment())
            .addToBackStack(null)
            .commit()
}

At first I though that this issue has something to so with the fact that fragment which is being navigated to has inputs as well. I tried navigating to other fragment with no input's and it resulted in the same thing.


Solution

  • I found out what was causing this issue, turns out it has nothing to do with the code but the Android Studio 3.1 Profiler Similar issue was mentioned here: https://www.queryoverflow.gdn/query/app-freeze-on-keypad-open-with-android-studio-3-1-update-27_49556735.html