为什么Android 5.0.2上会抛出NoSuchFieldException:ON_STOP?
我正在使用android.arch.lifecycle扩展来检测应用程序何时在后台或前台移动:为什么Android 5.0.2上会抛出NoSuchFieldException:ON_STOP?,android,android-lifecycle,Android,Android Lifecycle,我正在使用android.arch.lifecycle扩展来检测应用程序何时在后台或前台移动: @OnLifecycleEvent(Lifecycle.Event.ON_STOP) private void onAppBackgrounded() { Log.i(this.getClass().getName(), "onAppBackgrounded()"); } 除了运行在Android 5.0.2(SDK 21)上的设备外,该扩展工作正常,我在该设
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onAppBackgrounded() {
Log.i(this.getClass().getName(), "onAppBackgrounded()");
}
除了运行在Android 5.0.2(SDK 21)上的设备外,该扩展工作正常,我在该设备上遇到以下异常:
Caused by java.lang.NoSuchFieldException: ON_STOP
at java.lang.Class.getDeclaredField + 929(Class.java:929)
at libcore.reflect.AnnotationAccess.decodeValue + 685(AnnotationAccess.java:685)
at libcore.reflect.AnnotationAccess.toAnnotationInstance + 663(AnnotationAccess.java:663)
at libcore.reflect.AnnotationAccess.toAnnotationInstance + 641(AnnotationAccess.java:641)
at libcore.reflect.AnnotationAccess.getDeclaredAnnotation + 170(AnnotationAccess.java:170)
at java.lang.reflect.Method.getAnnotation + 301(Method.java:301)
at android.arch.lifecycle.ClassesInfoCache.createInfo + 124(ClassesInfoCache.java:124)
at android.arch.lifecycle.ClassesInfoCache.hasLifecycleMethods + 59(ClassesInfoCache.java:59)
at android.arch.lifecycle.Lifecycling.resolveObserverCallbackType + 137(Lifecycling.java:137)
at android.arch.lifecycle.Lifecycling.getObserverConstructorType + 119(Lifecycling.java:119)
at android.arch.lifecycle.Lifecycling.getCallback + 57(Lifecycling.java:57)
at android.arch.lifecycle.LifecycleRegistry$ObserverWithState.(LifecycleRegistry.java:2)
at android.arch.lifecycle.LifecycleRegistry.addObserver + 162(LifecycleRegistry.java:162)
请注意,
ProcessLifecycleOwner
是解决您的问题(“检测应用程序何时在后台或前台移动”)的更好方法。不过,这并不能解释这次崩溃。