显示使用nextFocusDown设置的键盘后切换Fragment时,Android应用程序崩溃 [英] Android app crashes when switching Fragment after showing a keyboard that is set with nextFocusDown

查看:422
本文介绍了显示使用nextFocusDown设置的键盘后切换Fragment时,Android应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以很好地处理多个片段,这些片段可以基于导航栏交互进行切换. 其中一个屏幕具有多个文本字段,我决定对这些字段进行排序,以便键盘"next"将通过使用nextFocusDown自动将用户带到下一个可填充的文本字段.最后一个会导致RadioGroup,导致显示完成"按钮.

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.

字段看起来像这样(它们在LinearLayout内,在NestedScrollView内,在另一个LinearLayout内,在根FrameLayout之下)

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.

如果我运行该应用程序,请选择一个字段,然后单击下一步"按钮,直到键盘自行消失-一切正常. 当我选择一个字段时,出现问题,键盘出现,并且我按下了导致键盘消失的android down按钮. 这样做之后,该屏幕上的所有内容看起来都很好,并且一切正常,直到我切换一个片段,然后应用程序崩溃,并且我在日志中看到了以下错误:

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.
由于我很确定它与nextFocusOn和键盘有关,因此我没有发布更多内容(这是一个很大的文件),如果您认为还需要其他任何内容,请告诉我,我将上载相关部分

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.

推荐答案

为我解决问题的是:

  1. 清理构建(Build-> Clean Project)
  2. 退出Android Studio
  3. 重新启动android设备
  4. 启动Android Studio(启动设备后)
  5. 运行

我不知道为什么它为我解决了这个问题(我主要是为iOS编写应用程序,而我从未遇到需要重启设备的错误...). 奇怪的是,我重新启动了Android Studio设备,并多次清理了项目-因此看来整个过程必须按此顺序进行.

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

这篇关于显示使用nextFocusDown设置的键盘后切换Fragment时,Android应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆