代码之家  ›  专栏  ›  技术社区  ›  jesobremonte

Android 8.0 Oreo在聚焦文本输入文本时崩溃

  •  105
  • jesobremonte  · 技术社区  · 7 年前

    TextInputEditText TextInputLayout ,应用程序因此崩溃 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)
    

    当我们使用android时 text输入text / 文本输入布局

    我们如何在不禁用设备上新的8.0自动填充服务的情况下防止崩溃的发生?

    8 回复  |  直到 6 年前
        1
  •  191
  •   Thomas Vos Jaydev Mehta    7 年前

    我也遇到了这个。事实证明,问题是由于在 EditText 嵌套在 TextInputLayout .

    我做了一些挖掘,在26.0.0 Beta 2的发行说明中找到了这个金块。 Android Support Release Notes June 2017

    这让我尝试在 文本输入布局 编辑文本 .

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

    here 我把书签弄混了。很抱歉发布了两次相同的答案。

        2
  •  26
  •   Gaurav Singh    7 年前

    将下面提到的属性添加到 EditText

    android:importantForAutofill="noExcludeDescendants"

        3
  •  12
  •   Endy Silveira    7 年前

    卢克·辛普森(LukeSimpson)几乎做到了,只是应该使用“style.xml”而不是“themes.xml”。

    我创建了一个带有版本限定符的新样式文件,目标是v26,以使其更清晰。
    只需复制并粘贴您的 AppTheme 对于v26/styles。xml和add editTextStyle 项目 EditTextStyle

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

    通过这种方式,您可以对 编辑文本,而无需更改布局文件。

        4
  •  6
  •   Luke Simpson    7 年前

    我使用了v26/主题。xml仅覆盖Oreo 8.0.0的EditText样式自动填充:

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

    // 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>
    
        5
  •  6
  •   Community CDub    4 年前

    您可以为 importantForAutofill here

    谢谢。

        6
  •  1
  •   Monika Saini    6 年前

    @卢克·辛普森是对的。你可以在主题中使用它。类似XML:-

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

    但是,我不得不把空标签也在app_风格。默认文件夹中的xml。否则,编辑文本的所有属性都被此覆盖,我的编辑文本无法正常工作。

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

    因此,自动填充属性在8.1中起作用。它将仅在8.0版本中被禁用,因为碰撞将在8.0版本中发生,并且已在8.1版本中修复。

        7
  •  0
  •   Prakash Bala    5 年前

    如果还有人想要” 暗示 “制造 应用程序:hintEnabled=“false” 文本输入布局

    <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>
    
        8
  •  0
  •   CHANDAN KUMAR    4 年前

    我也面临着这个问题,最后我们找到了安卓8.0和安卓8.1崩溃的原因。

    第一个原因(重要线索):xml中的空提示(android:hint=“”)导致oreo设备崩溃。请在整个项目搜索中删除editText中的此空提示。

    第二个原因(与上述解释相同):如果您使用了TextInputLayout,请确保您的editText提示应该显示在TextInputLayout内,否则您可以在editText内使用提示。

    希望这对你有帮助!!