Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app crashes when switching Fragment after showing a keyboard that is set with nextFocusDown

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.

like image 896
evenro Avatar asked Apr 10 '18 04:04

evenro


People also ask

How do I prevent the soft keyboard from pushing my view up in fragment?

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.

How do you fix an app that keeps crashing on Android studio?

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.

How do I find out why an app is crashing Android?

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.

Why is app crashes in emulator Android studio?

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.


3 Answers

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.

like image 31
Bartosz Avatar answered Oct 06 '22 01:10

Bartosz


The thing that solved the issue for me was:

  1. Clean the build (Build->Clean Project)
  2. Exit Android Studio
  3. Restart the android device
  4. Start Android Studio (AFTER DEVICE STARTED)
  5. Run

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...

like image 198
evenro Avatar answered Oct 06 '22 01:10

evenro


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.

like image 29
Igor Escodro Avatar answered Oct 06 '22 01:10

Igor Escodro