10

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.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
evenro
  • 2,626
  • 20
  • 35

4 Answers4

11

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

evenro
  • 2,626
  • 20
  • 35
  • Not a solution. This happens even if you restart the device – Jono Apr 20 '18 at 10:57
  • It appears to be a common issue that can be caused by multiple issues. I had to restart the device while simultaneously restarting android studio... there also might be a code issue (related to fragments being released from memory , wrong configurations etc. did you get the exact same error I did? – evenro Apr 20 '18 at 13:49
  • I spent a good while debugging my own code trying to figure what I'd done wrong .. to find it wasn't me! I can honestly say that Android platform / Android Studio is becoming less and less stable, with more gotchas and caveats than ever before. – Mark Apr 22 '18 at 19:00
  • @MarkKeen , the bug that seriously made me lose my mind was that hovering with the mouse above the screen resulted in code disappearing and files disappearing.. thank god for GIT.. something has to be done to fix it.. – evenro Apr 22 '18 at 19:07
  • @jonney Following the above steps solved the issue for me - maybe in your case something is different? – Mark Apr 22 '18 at 20:28
  • 1
    I had the same issue both on the emulator and on my Huawei device. Your magic steps saved me. – NewestStackOverflowUser May 17 '18 at 20:58
  • How can this happen ? this solution worked for me too.. AndroidStudio 3.1.2 – Pandiri Deepak Jun 06 '18 at 13:37
8

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.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Bartosz
  • 140
  • 1
  • 6
  • I don't have it enabled :( – evenro Apr 11 '18 at 01:36
  • Hmm, what I noticed also 1. I don't know why but it was crashing my app only when i had set `myRecycler.setNestedScrollingEnabled(false); ` 2. I looked into android API code, I found that comment in [file](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/inputmethod/InputConnection.java) {@link #closeConnection()}, which was introduced in {@link android.os.Build.VERSION_CODES#N} Maybe you should specified API target. – Bartosz Apr 11 '18 at 09:55
  • It happened to me also when a text was in screen that did not have a scroller at all... In another answer you'll find how I solved it.. seems to be a corruption in my case.. – evenro Apr 12 '18 at 01:51
  • Disabling advanced profiling helped for me too (was NullPointerException after leaving/clicking outside an EditText, AS 3.1.1) – allofmex Apr 26 '18 at 11:05
2

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.

Igor Escodro
  • 1,307
  • 2
  • 16
  • 30
1

For zero reason, the above solutions didn't work. For zero reason, starting an emulator alongside my device and launching the app on both at once fixed the issue.

This doesn't make any sense and whoever did this crap owes me an hour.

I hope I can save someone's time if this fixes the issue.

Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
  • 1
    Do you use a Huawei device by any chance? I've gotten a P10+ as a replacement of my Nexus 6P (battery issue), and I have to say that this is the worst development device I have ever owned. This is a huge pile of crap. (as a user it is fair, or even nice, but god I hate Huawei) – Benoit Duffez May 02 '18 at 21:21