如果我们之前的代码曾经设置的是界面打开就自动聚焦到EditText 输入框。
这样的需求在Android 8.0(API 26)之前不会有任何问题。
但是一旦当我们的Android 系统版本升级到8.1后,EditText 控件上就会引发崩溃。
错误日志如下所示:
java.lang.NullPointerException: activityToken
at android.os.Parcel.readException(Parcel.java:2010)
at android.os.Parcel.readException(Parcel.java:1950)
at android.view.autofill.IAutoFillManager$Stub$Proxy.startSession(IAutoFillManager.java:397)
at android.view.autofill.AutofillManager.startSessionLocked(AutofillManager.java:1012)
at android.view.autofill.AutofillManager.notifyViewEntered(AutofillManager.java:604)
at android.view.autofill.AutofillManager.notifyViewEntered(AutofillManager.java:575)
at android.view.View.notifyEnterOrExitForAutoFillIfNeeded(View.java:6980)
at android.view.View.onWindowFocusChanged(View.java:12059)
at android.widget.TextView.onWindowFocusChanged(TextView.java:9611)
at android.widget.AutoCompleteTextView.onWindowFocusChanged(AutoCompleteTextView.java:1099)
at android.view.View.dispatchWindowFocusChanged(View.java:12029)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1398)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3873)
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)
解决方案:
在方法执行的最前面加上这段神代码即可
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
getWindow().getDecorView().setImportantForAutofill(
View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
}
位置:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
getWindow().getDecorView().setImportantForAutofill(
View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
}
}
参考方案:https://stackoverflow.com/questions/47763756/autofill-framework-updates-8-1-cause-crash-to-edittext