102

After updating some of our devices to android 8.0 , upon focusing on a TextInputEditText field inside of a TextInputLayout, the app crashes with this Exception:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.getBoundsOnScreen(android.graphics.Rect)' on a null object reference
at android.app.assist.AssistStructure$WindowNode.(AssistStructure.java)
at android.app.assist.AssistStructure.(AssistStructure.java)
at android.app.ActivityThread.handleRequestAssistContextExtras(ActivityThread.java:3035)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1807)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

When we go to android Settings -> System -> Languages & input -> Advanced -> Auto-fill service -> None , then focusing on the TextInputEditText / TextInputLayout no longer crashes.

How can we prevent the crash from happening without having to disable the new 8.0 Auto-fill service on the devices?

sanjeev
  • 1,567
  • 13
  • 31
jesobremonte
  • 2,709
  • 2
  • 18
  • 25

8 Answers8

187

I ran into this too. It turns out the issue was caused by setting the hint text on the EditText nested inside the TextInputLayout.

I did some digging and found this nugget in the 26.0.0 Beta 2 release notes. Android Support Release Notes June 2017

TextInputLayout must set hints on onProvideAutofillStructure()

That led me to try setting the hint on the TextInputLayout instead of the nested EditText.

This resolved the crashing issue for me. Example:

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Some Hint Text"
    android.support.design:hintAnimationEnabled="true"
    android.support.design:hintEnabled="true"
    android.support.design:layout_marginTop="16dp">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.design.widget.TextInputLayout>

I posted this as an answer here as I mixed up bookmarks. Sorry for posting the same answer twice.

Thomas Vos
  • 11,085
  • 4
  • 24
  • 63
Azethoth
  • 2,216
  • 1
  • 11
  • 10
  • Will this work without android.support.design:hintAnimationEnabled and android.support.design:hintEnabled? I looked at TextInputLayout source, both attributes seem to be set as default when loading attributes. – androidguy Jul 21 '18 at 23:44
  • @androidguy yes it should work without those attributes. Those attributes tell the TextInputLayout to allocate space in the layout for those items(or at least that is what I have observed) – Azethoth Jul 23 '18 at 00:59
  • @Azethoth This solution doesn't retain the behaviour. In my case, I am using it for floating label animation. When I use it this way, the hint/label doesn't appear. All I can see is a textinput – KrishnaCA Oct 16 '18 at 09:47
  • @KrishnaCA that is quite weird. I use this approach everywhere in my app and it both shows the hint in the edit text and animates it as a floating label. I'm not sure what may be causing your issue. Can you post a code sample of your xml layout in a question and link me to it? I'd be happy to try and assist. – Azethoth Oct 16 '18 at 18:06
  • It’s the same as posted in your answer. The device that I tested for is Huawei – KrishnaCA Oct 16 '18 at 18:09
  • @KrishnaCA I wonder if it is something to do with whatever Huawei does for their flavor of Android. Have you tested on an emulator or other device? You can also avoid this crash by setting the hint on the edit text in code. The bug manifests when hint is set in xml. – Azethoth Oct 16 '18 at 19:09
  • it's not only with edittext, I had to remove hint from all nested views of TextInputLayout – Prabs Nov 20 '18 at 05:51
25

Add below mentioned attribute in your EditText:

android:importantForAutofill="noExcludeDescendants"

Gaurav Singh
  • 1,650
  • 12
  • 29
12

Luke Simpson almost make it right, just should use "styles.xml" instead of "themes.xml".

I created a new style file with version qualifier, aiming to v26, to make it clearer.
Just copy and paste your AppTheme for the v26/styles.xml and add editTextStyle items the EditTextStyle style.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="android:editTextStyle">@style/App_EditTextStyle</item>
    <item name="editTextStyle">@style/App_EditTextStyle</item>
</style>

<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
    <item name="android:importantForAutofill">noExcludeDescendants</item>
</style>

In this way you make this changes for all your EditTexts without needing to change your layout files.

Endy Silveira
  • 201
  • 3
  • 12
6

You can set any value for importantForAutofill with a style or in the XML it's fix for NPE when you focus the EditText but it's not fix if your long press the EditText and you click on AutoFill. I found a Bug Report about this bug here, please add a star and share your observations in the bug report also.

Thx.

Community
  • 1
  • 1
5

I used the v26/themes.xml to override the EditText style autofill only for Oreo 8.0.0:

<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
    <item name="android:importantForAutofill">noExcludeDescendants</item>
</style>

Note that I had to apply the style inline for each EditText in my layout xml for it to take effect. I tried to apply this change globally in my app theme but it didn't work for some reason.

// HAD TO DO THIS IN LAYOUT XML FOR EACH EDIT TEXT
<EditText
    style="@style/EditTextStyle"
    ... />


// THIS DIDN'T TAKE EFFECT IN THEMES XML (HAS BEEN ADDED TO MANIFEST)
<style name="APP_THEME" parent="@style/Theme.AppCompat.Light">
    <item name="android:editTextStyle">@style/EditTextStyle</item>
    <item name="editTextStyle">@style/EditTextStyle</item>
</style>
0

@Luke Simpson is right. You can use it in themes.XML like:-

<item name="editTextStyle">@style/AppEditTextStyle</item>

and then put
<style name="AppEditTextStyle" parent="Widget.AppCompat.EditText">
        <item name="android:importantForAutofill">auto</item>
 </style>

in V26/app_styles.xml

But, I had to put empty tag also in app_styles.xml in the default folder. Otherwise, all the properties of Edit text were getting overriding by this and my edit text was not working properly. And when you put importantForAutoFill property for v26 and you want autofill to work in 8.1, you can simply put

<style name="AppEditTextStyle" parent="Widget.AppCompat.EditText">
        <item name="android:importantForAutofill">auto</item>
    </style>

So, autofill property works in 8.1. It will be disabled just for 8.0 as the crash is hapenning in 8.0 and it has already been fixed in 8.1.

Monika Saini
  • 151
  • 5
0

If anyone still wants "hint" in "TextInputEditText" make app:hintEnabled="false" in TextInputLayout

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintEnabled="false"
    app:passwordToggleEnabled="true">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="password"
        android:inputType="textPassword" />

</com.google.android.material.textfield.TextInputLayout>
Prakash Bala
  • 215
  • 4
  • 12
0

I have also facing this issue and finally we got the reason of crash on android 8.0 and android 8.1.

first reason(important clue): empty hint(android:hint="") in xml leads to crash in oreo device.Please remove this empty hint in editText in whole project search.

second reason(same as above explain): make sure your editText hint should be show inside TextInputLayout if you have used TextInputLayout otherwise you can use hint inside editText.

Hope this helpe you !!

Thank you

CHANDAN KUMAR
  • 59
  • 1
  • 1