I have an app that was working nicely with multiple fragments that are switching based on a navigation bar interaction.
One of the screens has multiple text fields, and I decided to order these fields so the keyboard "next" would automatically take the user to the next fillable text field by using nextFocusDown
. The last one leads to RadioGroup
which results in the "done" button being shown.
The fields look like this (they are within LinearLayout
that is within a NestedScrollView
within another LinearLayout
that is under the root FrameLayout
):
<EditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName|textCapWords"
android:nextFocusDown="@id/lastName"
android:text="" />
<EditText
android:id="@+id/lastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName|textCapWords"
android:nextFocusDown="@id/email"
android:text="" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:nextFocusDown="@id/localId"
android:text="" />
There are many more fields, some include hints for text etc.
If I run the app, pick one field, and hit the "next" button until the keyboard disappears on its own - everything works well. The problem appears when I pick a field, the keyboard appears, and I hit the android down button that causes the keyboard to disappear. After doing so, everything within this screen looks good, and everything functions normally, until I switch a fragment, then the app crashes and I see in the logs these errors:
04-10 00:12:24.763 redacted E/AndroidRuntime: FATAL EXCEPTION: main
Process: redacted, PID: 24660
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:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
04-10 00:12:24.769 redacted E/UncaughtException: 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:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Any idea what's going on and how to avoid it?
P.S.
Since I am pretty sure it is related to the nextFocusOn and the keyboard, I didn't post more (it's a pretty big file), if you think anything is needed in addition, let me know, and I'll upload the relevant part.
Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.
The easiest way to fix an app that keeps crashing on your Android smartphone is to simply force stop it and open it again. To do this, go to Settings -> Apps and select the app that keeps crashing. Tap on the app's name and then tap on 'Force stop'. Now try opening the app again and see if it works well.
Select an app. On the left menu, select Quality > Android vitals > Crashes and ANRs. Near the center of your screen, use the filters to help you find and diagnose issues. Alternatively, select a cluster to get more details about a specific crash or ANR error.
An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java or Kotlin crashes if it throws an unhandled exception, represented by the Throwable class.
I faced with that bug today as well. I´m not sure if it will help in your case but it helped me. Based on answers in this topic: Android Studio 3.1 EditText StackOverflowError.
In my case it was enough just to disable advanced profiling
Run --> Edit Configuration --> Profiling Tab
In my case bug happen after changing conductor Controller from one with inputs to another. Error was exactly the same. And same as in related link i updated AS to 3.1.1 today.
The thing that solved the issue for me was:
I have no idea why it solved the issue for me (I mostly write apps for iOS, and I never experienced bugs that require a device restart...). The weird thing is that I restarted the device, Android Studio, and clean the projects many times - so it appears the whole process has to be in that order...
I had the same error using the Navigation from Android Architeture Components, which uses Fragment Transaction inside. The issue was that the EditText had text before the transaction and for some reason Android framework crashed.
The solution was setting all the EditText
to null
before doing the transaction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With