Android N 指纹识别 NullPointerException: Attempt toFragment.mNextAnim 的错误

测试报的bug是这样的:

已录制指纹,关闭指纹后重新开启指纹识别,设置停止运行

步骤如下:

A)Preconditions:
已添加指纹
B)Operation step:
1.设置》安全》锁屏方式》滑动解锁
2.安全》指纹》设置备用解锁方式
Expect resut:
可以正常进入指纹管理界面
DTest result:
提示设置停止运行

log上报的错误是这样的:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at com.android.settings.fingerprint.FingerprintSettings$FingerprintSettingsFragment.updateAddPreference(FingerprintSettings.java:374)
    at com.android.settings.fingerprint.FingerprintSettings$FingerprintSettingsFragment.-wrap4(FingerprintSettings.java)
    at com.android.settings.fingerprint.FingerprintSettings$FingerprintSettingsFragment$3.handleMessage(FingerprintSettings.java:212)       

这是最初报的bug
以后上面的莫名其妙不怎么报了,但是下面这个bug必报

java.lang.NullPointerException: Attempt to write to field 'int android.app.Fragment.mNextAnim' on a null object reference

去网上搜了半天说是因为fragment为空的时候hide remove 或是show了,但是这毕竟是谷歌大神写的源码,毫无为空的地方
因为它也不显示到底第几行报错,所以一度让人很头疼

后来我们老大给我说是这个原因

Like commit() but allows the commit to be executed after an activity’s state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.

大致意思是说我使用的 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为

onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的如果在保存完状态后

再给它添加Fragment就会出错。

解决办法就是把commit()方法替换成 commitAllowingStateLoss()就行了,其效果是一样的。

后来我将Setting中的所有commit()换成commitAllowingStateLoss()就好了

你可能感兴趣的:(技术总结)